如何使用C ++程序中的gnuplot绘制图形

时间:2015-03-06 04:39:52

标签: c++ gnuplot

以下是我用来将数据(x和y坐标)写入文件的代码。

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();

}

我想使用上面文件中的x和y坐标绘制图形&#34; Co_ordinates.txt&#34;我添加了gnuplot实用程序&#34; gnuplot_i.hpp&#34;来自https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp

我使用了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 = "");

我添加了以下代码来绘制图表

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

但是得到以下错误

错误:表达式列表在初始化程序[-fpermissive] |中被视为复合表达式 错误:从'int'|

类型的右值开始无效初始化'Gnuplot&amp;'类型的非const引用

我已尝试过几种形式的上述代码..但是收到错误。 请提出一些解决方案..

2 个答案:

答案 0 :(得分:8)

使用以下代码

可以轻松完成我所做的全部工作

system("gnuplot -p -e \"plot 'Co_ordinates.txt'\"");

答案 1 :(得分:3)

plotfile_xyGnuplot类的成员函数,因此要调用它,您需要Gnuplot的实例,例如:

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

文档的方式并不多,但您是否注意到有一个示例程序可以演示很多功能? https://code.google.com/p/gnuplot-cpp/source/browse/trunk/example.cc