如何使用 C++ 程序中的 gnuplot 绘制图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28892434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot a graph using gnuplot from C++ program
提问by user3851761
Following is the code I have used to write data (x and y coordinates)into a file.
以下是我用来将数据(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();
}
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.
我想使用上述文件“Co_ordinates.txt”中的 x 和 y 坐标绘制图形我从https://code.google.com/p/gnuplot-cpp/source/添加了 gnuplot 实用程序“gnuplot_i.hpp”浏览/主干/gnuplot_i.hpp。
I have used the following function defined in 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 = "");
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'|
错误:表达式列表在初始化器中被视为复合表达式 [-fpermissive]| 错误:从“int”类型的右值初始化“Gnuplot&”类型的非常量引用无效|
I have tried the above code in several forms.. but getting errors. Please suggest some solutions..
我已经尝试了几种形式的上述代码..但出现错误。请提出一些解决方案..
采纳答案by 1.618
plotfile_xy
is a member functionof the Gnuplot
class, so to call it you need an instance of Gnuplot
, for example:
plotfile_xy
是一个成员函数中的Gnuplot
类,所以调用它,你需要的实例Gnuplot
,例如:
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
文档的方式并不多,但是您是否注意到有一个示例程序演示了很多功能? https://code.google.com/p/gnuplot-cpp/source/browse/trunk/example.cc
回答by user3851761
The whole thing I have done can be easily done using the following code
使用以下代码可以轻松完成我所做的整件事
system("gnuplot -p -e \"plot 'Co_ordinates.txt'\"");