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

use*_*761 8 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();

}
Run Code Online (Sandbox Code Playgroud)

我想使用上面文件"Co_ordinates.txt"中的x和y坐标来绘制图形我已经从https://code.google.com/p/gnuplot-cpp/source/添加了gnuplot实用程序"gnuplot_i.hpp" 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 = "");
Run Code Online (Sandbox Code Playgroud)

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

const string s="Co_ordinates.txt";
Gnuplot& plotfile_xy(&s,1,2,'Grid');
Run Code Online (Sandbox Code Playgroud)

但是得到以下错误

错误:表达式列表在初始化程序[-fpermissive] |中被视为复合表达式 错误:从'int'|类型的右值开始,无效初始化'Gnuplot&'类型的非const引用

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

use*_*761 8

使用以下代码可以轻松完成我所做的全部工作

system("gnuplot -p -e \"plot 'Co_ordinates.txt'\"");
Run Code Online (Sandbox Code Playgroud)


1.6*_*618 4

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

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

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