Saturday, December 29, 2018

Curve Plotting with C++

In this article, some of the tools for plotting curves with C++ will be discussed with examples. They are


Gnuplot

Gnuplot can be installed on Ubuntu using the following commands.

$ sudo apt update
$ sudo apt install gnuplot


Entering the following command starts Gnuplot.

$ 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



Figure 1. Plotting sin(x) and cos2(x) to test gnuplot.


To use Gnuplot with a C++ program, we will use Jeremy Conlin's gnuplot-cpp [Con14, Gnu18]. For that,

gnuplot_i.hpp (https://github.com/yan9a/cecpp/blob/master/plotcpp/gnuplot/gnuplot_i.hpp)

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.

gplot.cpp (https://github.com/yan9a/cecpp/blob/master/plotcpp/gnuplot/gplot.cpp)

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



Figure 2. Output of gplot.cpp


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'\"");


Octave

Using 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.

oplot.cpp (https://github.com/yan9a/cecpp/blob/master/plotcpp/octaveplot/oplot.cpp)

ofunc.m (https://github.com/yan9a/cecpp/blob/master/plotcpp/octaveplot/ofunc.m)

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



Figure 3. Output of oplot.cpp


Matplotlib

You 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.

mplot.cpp (https://github.com/yan9a/cecpp/blob/master/plotcpp/matplot/mplot.cpp)

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



Figure 4. Output of mplot.cpp




References

[Mig18] Andrea Mignone. A Quick Guide to Gnuplot. 2018.
url: http://personalpages.to.infn.it/~mignone/Algoritmi_Numerici/gnuplot.pdf.

[Con14] Jeremy Conlin. gnuplot-cpp. 2014.
url: https://code.google.com/archive/p/gnuplot-cpp/.

[Gnu18] Gnuplot. gnuplot links. 2018.
url: http://www.gnuplot.info/links.html.

[Eve14] Benno Evers. matplotlib-cpp. 2014.
url: https://github.com/lava/matplotlib-cpp.

No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.