C++ 和 gnuplot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424455/
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
C++ and gnuplot
提问by Marco Orrù
This is my first post and I'm quite a novice on C++ and compiling in general.
这是我的第一篇文章,我是 C++ 和一般编译的新手。
I'm compiling a program which requires some graphs to be drawn. The program create a .dat file and then i should open gnuplot and write plot '.dat'. That's fine.
我正在编译一个需要绘制一些图形的程序。该程序创建了一个.dat 文件,然后我应该打开 gnuplot 并编写 plot '.dat'。没关系。
Is there a way to make gnuplot automatically open and show me the plot I need? I should use some system() function in the code to call gnuplot but how can I make him plot what I need?
有没有办法让 gnuplot 自动打开并显示我需要的图?我应该在代码中使用一些 system() 函数来调用 gnuplot 但我怎样才能让他绘制我需要的东西?
Sorry for my non-perfect English :s
抱歉我的英语不完美 :s
Thanks for the attention anyway!
总之谢谢关注!
采纳答案by Mr.Ree
Depending on your OS, you might be able to use popen().This would let you spawn a gnuplot process and just just write to it like any other FILE*.
根据您的操作系统,您或许可以使用popen()。这将让您生成一个 gnuplot 进程,并且只需像任何其他 FILE* 一样写入它即可。
If you have datapoints to plot, you can pass them inline with the plot "-" ...option. Similarly, you may want to explore set data style points/lines/linespoints/etcoptions.
如果您有要绘制的数据点,您可以使用绘图“-”...选项内联传递它们。同样,您可能想要探索设置数据样式点/线/线点/等选项。
Without pause or persist, gnuplot will terminate upon end-of-input-stream. In your example case, that would be when the end of the file is reached.
如果没有暂停或坚持,gnuplot 将在输入流结束时终止。在您的示例中,那将是到达文件末尾时。
To produce (write) an output file (graph), use:
要生成(写入)输出文件(图形),请使用:
set terminal png small
set output "filename.png"
There's lots of options to set terminal. Png is usually there. If not, perhaps gif, tiff, or jpeg?
有很多选项可以设置终端。Png通常在那里。如果没有,也许是 gif、tiff 或 jpeg?
Watch out for overwriting the file!
注意覆盖文件!
You may want to use set size 2,2to make a larger graph. Some set terminalvariants also allow you to specify the size.
您可能希望使用set size 2,2来制作更大的图形。某些设置终端变体还允许您指定大小。
回答by Andrew Wagner
I'm learning this today too. Here is a small example I cooked up.
我今天也在学习这个。这是我编写的一个小例子。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
ofstream file("data.dat");
file << "#x y" << endl;
for(int i=0; i<10; i++){
file << i << ' ' << i*i << endl;
}
file.close();
return 0;
}
Save that as plot.cpp and compile that with g++:
将其另存为 plot.cpp 并使用 g++ 编译它:
g++ plot.cpp -o plot
Run the program to create the .dat file:
运行程序以创建 .dat 文件:
./plot
Save the following gnuplot script as plot.plt:
将以下 gnuplot 脚本保存为 plot.plt:
set terminal svg enhanced size 1000 1000 fname "Times" fsize 36
set output "plot.svg"
set title "A simple plot of x^2 vs. x"
set xlabel "x"
set ylabel "y"
plot "./data.dat" using 1:2 title ""
Run the script with gnuplot to generate your .svg file:
使用 gnuplot 运行脚本以生成 .svg 文件:
gnuplot plot.plt
The resulting plot will be in plot.svg. If you leave out the first couple lines that specify the output, it will render in a window. Have fun!
结果图将在 plot.svg 中。如果您省略指定输出的前几行,它将在窗口中呈现。玩得开心!
回答by flolo
Sometimes it is as easy as one may think
有时它就像人们想象的一样简单
gnuplot file
where file is neither your data nor your result file, but a file with the command you would type in the commandline. Just enter there your commands you need (either constant file you have or generate it). After executing all commands in that file gnuplot exits.
其中 file 既不是您的数据也不是您的结果文件,而是一个包含您将在命令行中键入的命令的文件。只需在那里输入您需要的命令(您拥有或生成的常量文件)。执行该文件中的所有命令后,gnuplot 退出。
回答by ConcernedOfTunbridgeWells
Yes you can. You can make a file that has the commands you would otherwise type in to set up the plot and open gnuplot running from that file. This linkhas an article that explains how to do it. You can also output to an EPS or other graphical output formats and display the plot using another widget that reads in the file.
是的你可以。您可以创建一个文件,其中包含您本来要输入的命令来设置绘图并打开从该文件运行的 gnuplot。 这个链接有一篇文章解释了如何做到这一点。您还可以输出为 EPS 或其他图形输出格式,并使用另一个读取文件的小部件显示绘图。
回答by ConcernedOfTunbridgeWells
You may need to use the '-persist' flag to the command. I know that on *nix systems, this flag is required if you want the plot window to remain after the gnuplot process has completed and exited.
您可能需要在命令中使用“-persist”标志。我知道在 *nix 系统上,如果您希望在 gnuplot 进程完成并退出后保留绘图窗口,则需要此标志。
gnuplot -persist commands.gp
gnuplot -persist commands.gp
Also, you can put as many gnuplot commands as you like in the file. The file acts sort of like a batch script in this regard.
此外,您可以在文件中放置任意数量的 gnuplot 命令。在这方面,该文件有点像批处理脚本。
回答by KeithB
You might need to add a line
您可能需要添加一行
pause -1
This will show the plot until return has been pressed.
What you are probably seeing is that gnuplot runs and exits before the plot has time to be displayed.
这将显示绘图,直到按下返回键。
您可能看到的是 gnuplot 在绘图有时间显示之前运行并退出。
回答by Svante
You might need to set a terminal type. Read the gnuplot documentation about that.
您可能需要设置终端类型。阅读有关该内容的 gnuplot 文档。