May 14, 2012

How to call Gnuplot from C++

When I have do the simulation to our physics problem, I have many data files that I want to visualize. It's not an easy work, if you are a new C++ user like me. After searching around the internet, finally I got the solution pretty nice. Here is how.

1 download and intall

go to  gnuplot.info to download the windows installer and install it (The newest edition is 4.6 till this document). Make sure that you choose "add PATH to my system" in the process of installation, or you have to add by your own. (Thanks the gnuplot team adding this for us).

2 create a general purpose head file

create a new .h file with this following content(or you can download it here):

#ifndef GNUPLOT_H_
#define GNUPLOT_H_
#include <string>
#include <iostream>
using namespace std;
class Gnuplot {
public:
Gnuplot() ;
~Gnuplot();
void operator ()(const string & command);
// send any command to gnuplot
protected:
FILE *gnuplotpipe;
};
Gnuplot::Gnuplot() {
// with -persist option you will see the windows as your program ends
//gnuplotpipe=_popen("gnuplot -persist","w");
//without that option you will not see the window
 // because I choose the terminal to output files so I don't want to see the window
 gnuplotpipe=_popen("gnuplot","w");
if (!gnuplotpipe) {
cerr<< ("Gnuplot not found !"); }
}
Gnuplot::~Gnuplot() {
fprintf(gnuplotpipe,"exit\n");
_pclose(gnuplotpipe);
}
void Gnuplot::operator()(const string & command) {
fprintf(gnuplotpipe,"%s\n",command.c_str());
fflush(gnuplotpipe);
// flush is necessary, nothing gets plotted else
};
#endif

3 use the head file and plot

 now you have a class of gnuplot, so you can initialize it with object and send the command directly to gnuplot. If you don't know what I mean, check the example I use to test under Windows(example.cpp)

#include <iostream>
#include "gnuplot.h"
using namespace std;
int main(){
Gnuplot plot;
plot("plot sin(x)") ;
system("pause");
plot("plot cos(x)") ;
system("pause");
return 0;
}

Others:

The good side is that you can customize what you want to plot and make it automatically done for you.
The bad side is that it is the same usage as gnuplot itself. I mean, if you have to plot the simulation results, you have to output them then read in and plot the graph out. If you concerns only on the graph instead of the output data files, you will find that the most time-consuming thing for C++ is output files. If you have better solution for this( directly plot with a data structure), please let me know. I know there's some pipe written for this but they just don't work for me. Maybe you have better suggestion. Thanks.

5 comments:

Unknown said...

Hi,
I have tried to run gnuplot according to your description too.
If I run my application standalone, gnuplot window is opened, but when I run within VS2010 Debugger, then I do not see the gnuplot window. do you have any idea, what is wrong?
Thanks

ra said...

Hi, I have not tested this, but reading the header file, there is a line commented, with this explanation: //without that option you will not see the window.

ra said...
This comment has been removed by the author.
Unknown said...

Hello. I have tried the two scripts as they are. They work perfectly! BUT one has to delete the underscore in popen, I mean, "popen" and not "_popen". I don't know why, but that's what it is.
Thank you very much!!

Enrico Caffagni

Unknown said...

Hello, thank you for this code and suggestions on plotting using c++. I really like the concept of this solution but I am getting an error and I'm not sure why or how to fix it? If someone could please help it would be greatly appreciated. The output is:
sh: 1: gnuplot: not found
sh: 1: pause: not found