GnuplotGnuplot can be installed on Ubuntu using the following commands.
$ sudo apt update $ sudo apt install gnuplot
Entering the following command starts Gnuplot.
Information such as version number for Gnuplot will be displayed and you will be at the command prompt of gnuplot. As a testing, we will plot sin(x) and cos2(x) will be plotted on it again. Thereafter, the plots as illustrated in Figure. 1 should appear [Mig18]. You can enter 'q' to quit Gnuplot.
> plot sin(x) > replot cos(x)**2 > q
To use Gnuplot with a C++ program, we will use Jeremy Conlin's gnuplot-cpp [Con14, Gnu18]. For that,
needs to be included at the start in the program. I modified that header file to save the plot as a png format. Then, an instance of Gnuplot can be created and its methods can be called to produce various plots.
#include "gnuplot_i.hpp" ... Gnuplot g1; ... // generate x and y g1.set_style("lines").plot_xy(x,y,"2d plot");
A simple example to generate some data and to plot a png image can be found at the following link.
You can use the following commands to compile and run the program. The program produces the plot as illustrated in Figure. 2. A png image will also be produced in the same folder.
$ g++ gplot.cpp -o gplot $ ./gplot
As demonstrated in gsyscall.cpp, it is also possible to make a direct system call to Gnuplot in the C++ program.
system("gnuplot -p -e \"set datafile separator ',';plot 'test.csv'\"");
OctaveUsing MatLAB code to process data and to plot using Octave is also convenient. To install Octave, you can download the installer for Windows machine. For Debian Linux, it can be installed using the following commands.
$ sudo apt install octave octave-image octave-statistics octave-control
An example MatLAB function to read a data file and to plot the data, and a C++ program calling that MatLAB function can be found at the following links.
The output of the program is shown at Figure. 3. You can press any key to exit the program.
$ g++ oplot.cpp -o oplot $ ./oplot
MatplotlibYou can use the following command to install Matplotlib on Ubuntu.
$ sudo apt install python-matplotlib python-numpy python2.7-dev
We will use Benno Evers's matplotlib-cpp to plot using matplotlib with C++ [Eve14]. A simple example is shown below.
The program can be comipled and run using the following commands. The output of the program is shown in Figure. 4.
$ g++ mplot.cpp -I/usr/include/python2.7 -lpython2.7 -o mplot $ ./mplot
References[Mig18] Andrea Mignone. A Quick Guide to Gnuplot. 2018.
[Con14] Jeremy Conlin. gnuplot-cpp. 2014.
[Gnu18] Gnuplot. gnuplot links. 2018.
[Eve14] Benno Evers. matplotlib-cpp. 2014.