Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to plot a graph using gnuplot from C++ program

Tags:

c++

gnuplot

Following is the code I have used to write data (x and y coordinates)into 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 the graph using the x and y coordinates from the above file "Co_ordinates.txt" I have added gnuplot utility "gnuplot_i.hpp" from https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp .

I have used the following function defined in gnuplot_i.hpp

/// plot x,y pairs: x y
    ///   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 have added the following code to plot the graph

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

But getting following errors

error: expression list treated as compound expression in initializer [-fpermissive]| error: invalid initialization of non-const reference of type ‘Gnuplot&’ from an rvalue of type ‘int’|

I have tried the above code in several forms.. but getting errors. Please suggest some solutions..

like image 451
user3851761 Avatar asked Mar 06 '15 04:03

user3851761


Video Answer


2 Answers

The whole thing I have done can be easily done using the following code

system("gnuplot -p -e \"plot 'Co_ordinates.txt'\"");
like image 114
user3851761 Avatar answered Oct 17 '22 23:10

user3851761


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's not much in the way of documentation, but did you notice that there's a sample program that demonstrates a lot of the functions? https://code.google.com/p/gnuplot-cpp/source/browse/trunk/example.cc

like image 20
1.618 Avatar answered Oct 17 '22 23:10

1.618