Tutorial that teaches how to use the POSTPROCESS module to create graphs with GNUPLOT.
There is a chrono::postprocess::ChGnuPlot class that helps you to create .gpl gnuplot scripts directly from your cpp program.
Of course you could create the gpl script by yourself, save a .dat file from your program, and then launch gnuplot by hand, but this class generates the script for you and makes the graph creation easier. Also, it automatically calls gnuplot.
There are two prerequisites:
- GNUPLOT must be installed on your computer. We support releases from v.4.6. You can install either 32 or 64 bit versions from their site.
- the
gnuplot
command must be accessible from the shell. On Linux, Unix etc. this is ok by default. On Windows, you must remember to add the bin/ directory ofgnuplot.exe
to your PATH. That is, if all is ok, when you typegnuplot
in your cmd window, the gnuplot should start.
In the following you can see some examples of using the ChGnuPlot class.
Example 1
The most low-level way of using the ChGnuPlot class: use the the SetCommand() or alternatively the << operator, to build the .gpl script.
Such script will be saved on disk with the specified .gpl name.
When the mplot object goes out of scope and is deleted at the end of this example, the script is saved on disk and GNUplot is launched.
You should see the following windows that opens:
... if you open a shell (a DOS cmd window in Windows) and type
gnuplot
, does gnuplot start? If the program is not found, you should install gnuplot. ... make sure you can access it from the shell (add it to your PATH in windows)
... in case the .gpl command script contains errors, you can see which is the offending statement by opening a shell, go to the current working directory of the demo_POST_gnuplot executable using cd, and entering
gnuplot __tmp_gnuplot_1.gpl
where __tmp_gnuplot_1.gpl is the name of the gpl file, here is for this example. When executed it will prompt script errors, if any. Example 2
Learn how to use the Open... functions to define the output terminal. The gnuplot utility can create plots in windows, or it can save them as EPS or JPG or PNG or other formats.
This is a demo that opens two plots in two windows and save the second to an EPS file too.
Note that we use SetGrid()
, SetLabelX()
, SetLabelY()
. There are other functions like these. They are shortcuts to alleviate you from entering complex gnuplot commands. For instance mplot.SetLabelX("x");
is equivalent to mplot << "set xlabel \"x"";
, etc.
Then we redirect a first plot to a window n.0:
We redirect a following plot to a window n.1:
Finally we redirect a plot to an .EPS Postscript file, that can be used, for example, in LaTeX documents. Note that here we do not issue another plot command, but we rather use Replot()
, that repeats the last plot command (the one of the window n.1).
You should see the following two plots in two separate windows:
Example 3
Learn how to use the Plot()
shortcut functions, for easy plotting from a .dat file (an ascii file with column ordered data).
Of course you could do this by just adding gnuplot commands such as
etc., but the Plot()
functions make this easier.
Step 1: create a .dat file with three columns of demo data:
Step 2: Create the plot.
Note the Plot() shortcut: In this case you pass the .dat filename, the columns IDs, title and custom settings that define the plotting line type, width, etc. (see the gnuplot documentation)
Note that you can have multiple Plot() calls for a single Output, they will be overlapped as when you use commas in gnuplot: plot ... , ... , ...
. For instance, here overlap a 2nd plot to the previous one:
You should see the following plot:
Example 4
To make things even simpler, the Plot() shortcut function can be used directly on embedded data, without needing to save a .dat file.
One can use, for instance:
- a pair of x,y vectors (use chrono::ChVectorDynamic column matrices),
- chrono::ChFunction y(x) objects,
- chrono::ChFunctionInterp
- columns from a chrono::ChMatrixDynamic, etc.
The data values will be saved embedded in the .gpl file.
Note, the Replot() command does not work with embedded data.
Let's begin with creating some example data:
Now create the plot. Note the Plot() shortcuts.
You should see the following plot: