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

11

以下是我用来写入数据(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();

}

我想使用上述文件"Co_ordinates.txt"中的x和y坐标绘制图表,我已经添加了gnuplot实用程序"gnuplot_i.hpp",其来源于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');

但是出现以下错误:

error: 表达式列表在初始化器中被视为复合表达式[-fpermissive]| error: 非const引用类型的无效初始化‘Gnuplot&’从类型为‘int’的右值|

我已经尝试了上述代码的几种形式,但是仍然出现错误。 请提供一些解决方案..


我认为你写的 Gnuplot& plotfile_xy(&s,1,2,'Grid'); 这行代码,编译器看作是函数声明而不是调用。 - 1.618
2个回答

8
我已经完成的整个事情可以使用以下代码轻松实现。

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


4

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接