C语言 如何通过c程序绘制数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14311640/
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 data by c program?
提问by CrazyHorse
I am a mechanical engineer who has only limited knowledge in C programming. I wrote some code in order to make simulations, and I want to visualize the simulation results. At the moment I am using Dev-Cfor writing my codes. With fopenand fprintfcommands I generate a .datfile which includes the results. Then I open GNUPLOTprogram and import my .datfile to plot the results. This takes time and I have to wait till the end of the simulation. Is there an easy way to connect my plotter with Dev-C, so my plotter starts plotting data during the simulation? Any library or etc. ?
我是一名机械工程师,对 C 编程的知识有限。我写了一些代码来进行模拟,我想将模拟结果可视化。目前我正在Dev-C用于编写我的代码。使用fopen和fprintf命令我生成一个.dat包含结果的文件。然后我打开GNUPLOT程序并导入我的.dat文件以绘制结果。这需要时间,我必须等到模拟结束。是否有一种简单的方法可以将我的绘图仪与 连接Dev-C,以便我的绘图仪在模拟过程中开始绘制数据?任何图书馆等?
回答by Edgar Bonet
Since you already know gnuplot, the simplest thing to do may be to just call gnuplot from your program and pipe the data to it:
由于您已经了解 gnuplot,因此最简单的方法可能是从您的程序中调用 gnuplot 并将数据通过管道传输给它:
FILE *gnuplot = popen("gnuplot", "w");
fprintf(gnuplot, "plot '-'\n");
for (i = 0; i < count; i++)
fprintf(gnuplot, "%g %g\n", x[i], y[i]);
fprintf(gnuplot, "e\n");
fflush(gnuplot);
回答by James Black
OK, one solution, as you are writing out to a file, would be to just make a system()call when you write out to the file, and call gnuplot.
好的,system()当您写出文件时,一种解决方案是在写出文件时进行调用,然后调用 gnuplot。
But, that means you should change the filename each time, but I fear if you do that that it won't look correct, since you are sending small amounts of data each time.
但是,这意味着您每次都应该更改文件名,但我担心如果您这样做,它看起来会不正确,因为您每次都发送少量数据。
http://www.gnu.org/software/libc/manual/html_node/System-Calls.html
http://www.gnu.org/software/libc/manual/html_node/System-Calls.html
I have never used gnuplot, but if you look at this page (http://gnuplot-tricks.blogspot.com/) you may find some tricks that would enable this to work well.
我从未使用过 gnuplot,但是如果您查看此页面 ( http://gnuplot-tricks.blogspot.com/),您可能会发现一些可以使其正常工作的技巧。
But, I think, unless you are going to plot everything yourself, and skip gnuplot, then you may just need to wait, as you are.
但是,我认为,除非您打算自己绘制所有内容并跳过 gnuplot,否则您可能只需要等待,就像您一样。
You may find the C interface to gnuplot may help you:
您可能会发现 gnuplot 的 C 接口可以帮助您:
回答by kallikak
回答by paddy
I had a wee look around to see what other people have done regarding real-time plotting in gnuplot, and found this:
我环顾四周,看看其他人对 gnuplot 中的实时绘图做了什么,并发现了这一点:
http://users.softlab.ntua.gr/~ttsiod/gnuplotStreaming.html
http://users.softlab.ntua.gr/~ttsiod/gnuplotStreaming.html
There's a little Perl script that can drive the plotting, and you just pipe your information in.
有一个小的 Perl 脚本可以驱动绘图,您只需输入信息即可。
Since your data is being written to file, you may want to tail -f yourdata.datand pipe that into the real-time plotter.
由于您的数据正在写入文件,您可能希望将tail -f yourdata.dat其通过管道传输到实时绘图仪中。
Also, because you're using the stdio file calls, you'd need to flush regularly (by calling fflush)
此外,因为您使用的是 stdio 文件调用,所以您需要定期刷新(通过调用fflush)
Obviously your simulation would be running in the background or in another shell. That way you could break out of the plotting at any time without interrupting the simulation.
显然,您的模拟将在后台或另一个 shell 中运行。这样您就可以在不中断模拟的情况下随时退出绘图。
Hope that's of some use to you.
希望这对你有用。
回答by Martin Johansen
pbPlots is very easy to use and works with all C compilers.
pbPlots 非常易于使用并且适用于所有 C 编译器。
Download pbPlots.c/h and supportLib.c/h from the github pageand include them in your build.
从github 页面下载 pbPlots.c/h 和 supportLib.c/h并将它们包含在您的构建中。
Here is a complete example that will produce a plot in a PNG-file.
这是一个完整的示例,它将在 PNG 文件中生成一个绘图。
#include "pbPlots.h"
#include "supportLib.h"
int main(){
double x [] = {-1, 0, 1, 2, 3, 4, 5, 6};
double y [] = {-5, -4, -3, -2, 1, 0, 1, 2};
RGBABitmapImageReference *imageRef = CreateRGBABitmapImageReference();
DrawScatterPlot(imageRef, 600, 400, x, 8, y, 8);
size_t length;
double *pngData = ConvertToPNG(&length, imageRef->image);
WriteToFile(pngData, length, "plot.png");
return 0;
}
There are a lot more options available, these are described on the github page.
有更多可用选项,这些在 github 页面上进行了描述。


