Description

Class for plotting data with GNUplot.

This is a basic utility class which saves a temporary gpl file on disk and then calls the GNUplot utility from the system shell, so it does not require to link GNUplot libraries. If the GNUplot is not installed, simply nothing happens. Notes:

  • requires GNUplot version 4.6 or newer.
  • the GNUplot executable must be available from a command window (make sure the executable inb is in the search path).
  • if no plot is displayed, open a cmd shell, type "gnuplot __tmp_gnuplot.gpl -persist" and see which error is displayed.

#include <ChGnuPlot.h>

Public Member Functions

 ChGnuPlot (const std::string &filename="__tmp_gnuplot.gpl")
 
void SetPersist (bool state)
 Enable/disable persistent plots (default: true). More...
 
void SetCommand (const std::string &command)
 Add arbitrary GnuPlot commands in the gnuplot script. More...
 
ChGnuPlotoperator<< (const std::string &command)
 This is equivalent to SetCommand().
 
void SetColorSequence (unsigned int num)
 Set default color sequence in plots. More...
 
void Plot (ChVectorDynamic<> &x, ChVectorDynamic<> &y, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from two dynamic Eigen vectors.
 
void Plot (ChMatrixConstRef data, int colX, int colY, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from two columns of a matrix.
 
void Plot (ChFunctionInterp &fun_table, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from a ChFunctionInterp.
 
void Plot (const std::vector< double > &vals_x, const std::vector< double > &vals_y, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from two vectors.
 
void Plot (ChFunction &funct, double xmin, double xmax, double dx, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from a generic ChFunction. More...
 
void Plot (ChFunction &funct, int der_order, double xmin, double xmax, double dx, const std::string &title="", const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from a generic ChFunction's 0th, 1st, 2nd, or 3rd derivative.
 
void Plot (const std::string &datfile, int colX, int colY, const std::string &title, const std::string &customsettings=" with lines ")
 Plot 2D (x,y) data from an external file.
 
void StartSubplot (int rows, int cols, int idx)
 Start subplot in current figure. More...
 
void EndSubplot ()
 End current subplot layout. More...
 
void SetTitle (const std::string &label)
 Add a plot title.
 
void SetLabelX (const std::string &label)
 Add a label to the X axis.
 
void SetLabelY (const std::string &label)
 Add a label to the Y axis.
 
void SetLabelY2 (const std::string &label)
 Add a label to the Y2 axis.
 
void SetLabelZ (const std::string &label)
 Add a label to the Z axis.
 
void SetLegend (const std::string &customsettings)
 Show legend and set optional parameters. More...
 
void HideLegend ()
 Hide plot legend.
 
void SetGrid (bool dashed=true, double linewidth=1.0, const ChColor &color=ChColor(0, 0, 0))
 Enable and specify parameters for a plot grid.
 
void SetRangeX (double xmin, double xmax, bool automin=false, bool automax=false)
 Set the X data range.
 
void SetRangeY (double ymin, double ymax, bool automin=false, bool automax=false)
 Set the Y data range.
 
void SetRangeY2 (double ymin2, double ymax2, bool automin=false, bool automax=false)
 Set the Y2 data range.
 
void SetRangeZ (double zmin, double zmax, bool automin=false, bool automax=false)
 Set the Z data range.
 
void SetAspectRatio (double val)
 Set aspect ratio.
 
void SetAxesEqual (bool axequal)
 Set axes to equal size (i.e., square box) or restore default.
 
void SetOutputWindowTitle (const std::string &label)
 Set output window title.
 
void SetCanvasSize (int width, int height)
 Set canvas size.
 
void Replot ()
 Replot last plots (since the last time any of the functions Output*** was used).
 
void OutputWindow (int windownum=0, bool reset_text=false)
 Set plot in an interactive window. More...
 
void OutputPNG (const std::string &filename, int sizex=400, int sizey=300)
 Save plot in a PNG file. More...
 
void OutputPDF (const std::string &filename)
 Save plot in a PDF file. More...
 
void OutputEPS (const std::string &filename, double inchsizex=4, double inchsizey=3, bool color=true)
 Save plot in a EPS file. More...
 
void OutputCustomTerminal (const std::string &filename, const std::string &terminalsetting)
 Save plot in a custom terminal. More...
 
void FlushPlots ()
 Close current plot command (e.g., for use in multiplot).
 

Protected Member Functions

void FlushPlots (std::string &script)
 
void ExecuteGnuplot (std::string &script)
 
std::string col_to_hex (ChColor color)
 

Protected Attributes

std::string m_gpl_filename
 gnuplot file name
 
std::string m_commandfile
 gnuplot script
 
bool m_persist = true
 prevent window closure after plotting
 
bool m_subplots = false
 activate subplot mode
 
int m_curr_idx = 0
 current subplot index
 
int m_end_idx = 0
 index of last subplot
 
std::unordered_map< int, std::vector< ChGnuPlotDataplot > > m_map_plots
 index of subplot in figure, multiplots inside subplot
 

Member Function Documentation

◆ EndSubplot()

void chrono::postprocess::ChGnuPlot::EndSubplot ( )
inline

End current subplot layout.

Call this after all plots set by StartSubplot() have been completed.

◆ OutputCustomTerminal()

void chrono::postprocess::ChGnuPlot::OutputCustomTerminal ( const std::string &  filename,
const std::string &  terminalsetting 
)
inline

Save plot in a custom terminal.

For instance try terminalsetting ="set terminal svg size 350,262 fname 'Verdana' fsize 10" Call this before Plot() statements. Otherwise call Replot() just after.

◆ OutputEPS()

void chrono::postprocess::ChGnuPlot::OutputEPS ( const std::string &  filename,
double  inchsizex = 4,
double  inchsizey = 3,
bool  color = true 
)
inline

Save plot in a EPS file.

Call this before Plot() statements. Otherwise call Replot() just after.

◆ OutputPDF()

void chrono::postprocess::ChGnuPlot::OutputPDF ( const std::string &  filename)
inline

Save plot in a PDF file.

Call this before Plot() statements. Otherwise call Replot() just after.

◆ OutputPNG()

void chrono::postprocess::ChGnuPlot::OutputPNG ( const std::string &  filename,
int  sizex = 400,
int  sizey = 300 
)
inline

Save plot in a PNG file.

Call this before Plot() statements. Otherwise call Replot() just after.

◆ OutputWindow()

void chrono::postprocess::ChGnuPlot::OutputWindow ( int  windownum = 0,
bool  reset_text = false 
)
inline

Set plot in an interactive window.

For multiple windows, call this with increasing windownum, interleaving with Plot() statements etc. Call this before Plot() statements. Otherwise call Replot() just after.

◆ Plot()

void chrono::postprocess::ChGnuPlot::Plot ( ChFunction funct,
double  xmin,
double  xmax,
double  dx,
const std::string &  title = "",
const std::string &  customsettings = " with lines " 
)
inline

Plot 2D (x,y) data from a generic ChFunction.

Note that if the ChFunction is of type ChFunctionInterp there is a specific Plot() function

◆ SetColorSequence()

void chrono::postprocess::ChGnuPlot::SetColorSequence ( unsigned int  num)
inline

Set default color sequence in plots.

Available options:

  • 0: gnuplot default
  • 1: matplotlib
  • 2: matlab.

◆ SetCommand()

void chrono::postprocess::ChGnuPlot::SetCommand ( const std::string &  command)
inline

Add arbitrary GnuPlot commands in the gnuplot script.

Basically you would just need calls to SetCommand() followed by an Output() at the end, however to make things easier, there are some shortcut functions such as SetRangeX(), SetGrid(), Plot(), etc. which populate the command file.

◆ SetLegend()

void chrono::postprocess::ChGnuPlot::SetLegend ( const std::string &  customsettings)
inline

Show legend and set optional parameters.

Example: SetLegend("bottom right box opaque")

◆ SetPersist()

void chrono::postprocess::ChGnuPlot::SetPersist ( bool  state)
inline

Enable/disable persistent plots (default: true).

If false, the output window will close soon after the plot. For plotting in windows, it is better to use the default 'true' setting. When plotting to a file (EPS, PNG, PDF), this setting is usually set to 'false'.

◆ StartSubplot()

void chrono::postprocess::ChGnuPlot::StartSubplot ( int  rows,
int  cols,
int  idx 
)
inline

Start subplot in current figure.

Overall subplot layout is provided as

  • total number of rows
  • total number of columns
  • index of current subplot. Notes:
  • indices must be given in ascending order (starting from 0)
  • indices must cover all specified subplots (i.e., num indices = rows * cols)
  • requires manual call to EndSubplot() when all subplots have been completed.

The documentation for this class was generated from the following file:
  • /builds/uwsbel/chrono/src/chrono_postprocess/ChGnuPlot.h