How to plot data by c program?

北城余情 提交于 2020-07-02 06:02:12

问题


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-C for writing my codes. With fopen and fprintf commands I generate a .dat file which includes the results. Then I open GNUPLOT program and import my .dat file 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. ?


回答1:


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:

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



回答2:


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.

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

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.

But, I think, unless you are going to plot everything yourself, and skip gnuplot, then you may just need to wait, as you are.

You may find the C interface to gnuplot may help you:

http://ndevilla.free.fr/gnuplot/




回答3:


I've been using PLPlot for plotting from C and have found it both effective and easy. It's cross platform, open source, and supports a rich array of plot capabilities. I'd recommend having a look at the examples to get started.




回答4:


I had a wee look around to see what other people have done regarding real-time plotting in gnuplot, and found this:

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.

Since your data is being written to file, you may want to tail -f yourdata.dat and pipe that into the real-time plotter.

Also, because you're using the stdio file calls, you'd need to flush regularly (by calling 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.

Hope that's of some use to you.




回答5:


pbPlots is very easy to use and works with all C compilers.

Download pbPlots.c/h and supportLib.c/h from the github page and include them in your build.

Here is a complete example that will produce a plot in a PNG-file.

#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.



来源:https://stackoverflow.com/questions/14311640/how-to-plot-data-by-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!