How to build a graph using gnuplot from a C ++ program

Below is the code I used to write the data (x and y coordinates) to a file.

void display(){ fstream out; outfile.open("Co_ordinates.txt",fstream::out | fstream::trunc); outfile.precision(6); for(int i=0;i<3000; i++){ outfile<<fixed<<x[i]<<" "<<fixed<<y[i]<<endl; } out.close(); } 

I want to plot using the x and y coordinates from the above file "Co_ordinates.txt". I added the gnuplot utility "gnuplot_i.hpp" from https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp .

I used the following function defined in gnuplot_i.hpp

 /// plot x,y pairs: xy /// from file Gnuplot& plotfile_xy(const std::string &filename, const unsigned int column_x = 1, const unsigned int column_y = 2, const std::string &title = ""); 

I added the following code to plot

 const string s="Co_ordinates.txt"; Gnuplot& plotfile_xy(&s,1,2,'Grid'); 

But getting the following errors

error: expression list processed as a compound expression in the initializer [-fpermissive] | error: invalid initialization of non-constant reference of type 'Gnuplot & from rvalue of type' int |

I tried the above code in several forms, but got errors. Please suggest some solutions.

+7
c ++ gnuplot
source share
2 answers

plotfile_xy is a member function of the Gnuplot class, so to call it you need an instance of Gnuplot , for example:

 Gnuplot gp("lines"); //using the parameters from your code gp.plotfile_xy(&s,1,2,'Grid'); 

There is not much in the documentation, but have you noticed that there is an example program demonstrating many functions? https://code.google.com/p/gnuplot-cpp/source/browse/trunk/example.cc

+2
source share

Everything I have done can be easily done using the following code

 system("gnuplot -p -e \"plot 'Co_ordinates.txt'\""); 
+8
source share

All Articles