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 persist)
 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 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 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 Replot ()
 Replot last plots (since the last time any of the functions Output*** was used).
 
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 ymin, double ymax, 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 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 SetGrid (bool dashed=true, double linewidth=1.0, const ChColor &mcolor=ChColor(0, 0, 0))
 Enable and specify parameters for a plot grid.
 
void HideLegend ()
 Hide plot legend.
 
void SetLegend (const std::string &customsettings)
 Show legend and set optional parameters. More...
 
void SetAxesEqual ()
 Set axes to equal size (i.e., square box).
 
void OutputWindow (int windownum=0)
 Set plot in a 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 gpl_filename
 
std::string commandfile
 
std::vector< ChGnuPlotDataplot > plots
 
bool persist
 

Member Function Documentation

◆ 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)
inline

Set plot in a 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

◆ 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  persist)
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'.


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