C++ 和 gnuplot
这是我的第一篇文章,我对 C++ 和编译总体来说还是个新手。
我正在编译一个需要绘制一些图表的程序。 该程序创建一个 .dat 文件,然后我应该打开 gnuplot 并编写绘图“.dat”。 没关系。
有没有办法让 gnuplot 自动打开并显示我需要的图? 我应该在代码中使用一些 system() 函数来调用 gnuplot 但我怎样才能让他绘制我需要的东西呢?
抱歉我的英语不完美:
无论如何还是感谢您的关注!
This is my first post and I'm quite a novice on C++ and compiling in general.
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.
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?
Sorry for my non-perfect English :s
Thanks for the attention anyway!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您的操作系统,您也许可以使用 popen()。这将让您生成一个 gnuplot 进程,并且只需像任何其他 FILE* 一样写入它即可。
如果您有要绘制的数据点,您可以使用 plot "-" ... 选项内联传递它们。 同样,您可能想探索设置数据样式点/线/线点/等选项。
如果没有暂停或坚持,gnuplot 将在输入流结束时终止。 在您的示例中,这就是到达文件末尾的时间。
要生成(写入)输出文件(图形),请使用:
设置终端有很多选项。 通常是有PNG的。 如果不是,也许是 gif、tiff 或 jpeg?
小心覆盖文件!
您可能需要使用设置大小 2,2 来制作更大的图表。 一些设置终端变体还允许您指定大小。
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*.
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/etc options.
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.
To produce (write) an output file (graph), use:
There's lots of options to set terminal. Png is usually there. If not, perhaps gif, tiff, or jpeg?
Watch out for overwriting the file!
You may want to use set size 2,2 to make a larger graph. Some set terminal variants also allow you to specify the size.
我今天也学这个。 这是我编写的一个小例子。
将其另存为plot.cpp并使用g++进行编译:
运行程序以创建.dat文件:
将以下gnuplot脚本另存为plot.plt:
使用gnuplot运行脚本以生成.svg文件:
生成的绘图将在情节.svg。 如果省略指定输出的前几行,它将在窗口中呈现。 玩得开心!
I'm learning this today too. Here is a small example I cooked up.
Save that as plot.cpp and compile that with g++:
Run the program to create the .dat file:
Save the following gnuplot script as plot.plt:
Run the script with gnuplot to generate your .svg file:
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!
有时,这就像人们想象的那样简单,
其中文件既不是您的数据也不是您的结果文件,而是包含您在命令行中键入的命令的文件。 只需在此处输入您需要的命令(您拥有的常量文件或生成的常量文件)。
执行该文件中的所有命令后,gnuplot 退出。
Sometimes it is as easy as one may think
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.
是的你可以。 您可以创建一个文件,其中包含您要输入的命令来设置绘图并打开从该文件运行的 gnuplot。 此链接有一篇文章解释了如何执行此操作。 您还可以输出为 EPS 或其他图形输出格式,并使用读取文件的另一个小部件显示绘图。
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 link has 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.
您可能需要在命令中使用“-persist”标志。 我知道在 *nix 系统上,如果您希望在 gnuplot 进程完成并退出后保留绘图窗口,则需要此标志。
gnuplot -persist Commands.gp
此外,您可以在文件中放置任意数量的 gnuplot 命令。 在这方面,该文件的行为有点像批处理脚本。
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.
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 在绘图显示之前运行并退出。
You might need to add a line
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 文档。
You might need to set a terminal type. Read the gnuplot documentation about that.