web-dev-qa-db-ja.com

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

}

上記のファイル「Co_ordinates.txt」のx座標とy座標を使用してグラフをプロットしたい https://code.google.com/p/gnuplot-からgnuplotユーティリティ「gnuplot_i.hpp」を追加しました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&」の非const参照の初期化が無効です|

上記のコードをいくつかの形式で試しましたが、エラーが発生します。いくつかの解決策を提案してください。

8
user3851761

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

4
1.618

私が行ったすべてのことは、次のコードを使用して簡単に行うことができます

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