How to plot data by c program?
Solution 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);
Solution 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/
Solution 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.
Solution 4
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.
Related videos on Youtube
CrazyHorse
A thermal engineer who is trying to learn c programming in order to make his own simulation programs.
Updated on December 07, 2020Comments
-
CrazyHorse over 3 years
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. Withfopen
andfprintf
commands I generate a.dat
file which includes the results. Then I openGNUPLOT
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 withDev-C
, so my plotter starts plotting data during the simulation? Any library or etc. ?-
James Black over 11 yearsDo you have data to plot if the simulation isn't done?
-
ArjunShankar over 11 yearsWhat exactly takes time? Manually executing
gnuplot
and importing the data? Or doesgnuplot
itself take a lot of time plotting it? If its the first, you could easily get rid of it by executinggnuplot
from the simulator itself. e.g. in POSIX-likes, there is theexec
function. -
Hot Licks over 11 yearsI wonder if you couldn't use a pipe instead of a file.
-
Eddy_Em over 11 yearsAs I know, such popular plotting libraries like gnuplot & mathGL needs all data be ready before plotting.
-
CrazyHorse over 11 yearsHi all, thanks for replying. I will give quick info. The thing what takes time is everytime writing the gnuplot commands manually. By some commands I wrote in my C programs, I want gnuplot to automatically plot my data. Since it is hard, it is not really important whether during or end of simulation... Since the other things I read here, I am working on "_popen" command. But by somehow the program says: "popen undeclared (first use in this function)". (I am using win7 and I added #include <stdio.h>) Any ideas?
-
-
mgilson over 11 yearsI've used this one in a unix environment and when using cygwin on windows. It always worked for me :).
-
CrazyHorse over 11 years@Edgar Hi Edgar, thanks for answer. Calling gnuplot from my program and piping the data to it is exactly what I want to do. I added your code into my program (ofcourse I also changed X[i] and y[i]). But I got this compiling error: "'popen' undecleared (first use in this function). I am using Win 7, do you have any idea why I got this error? Thanks.
-
CrazyHorse over 11 yearsHi Paddy, thanks for answer. I checked the webpage you linked. Actually I was using this method (preparing make file, bash file) during my master work in a company. THere we were using Linux and the programs were all set by IT. Now in my personal computer I use Windows, so I need to learn everything (with my poor knowledge) this time for windows platform. Is there anyway to do those things in windows?
-
paddy over 11 yearsThe most promising way is to install Cygwin, but you could have a crack at installing Perl for Windows. There's a ported version kicking around. It's nice if you can stick to the tools and methods that you know rather than learn a whole new (crippled, in the case of Windows) toolset.
-
Edgar Bonet over 11 yearsSince popen() is POSIX, on Windows you may try with an underscore: _popen().
-
CrazyHorse over 11 years@Edgar, That also doesn't work. The code you wrote seems the easiest approach to my goal but I can't compile it :)
-
sitilge about 7 yearsPrepending
set nokey;
to plot removes the annoying legend. -
le_lemon over 3 yearsI added the pbPlots.c/h and supportLib.c/h files to a CLion project but when I try to run your example I get an error: C:/Users/flori/CLionProjects/Plottest/main.c:9: undefined reference to `CreateRGBABitmapImageReference' (+ a lot more unresolved references) ... what did I do wrong ?
-
Martin Johansen over 3 years@le_lemon, did you include pbPlots.c and supportLib.c in the build? In C, the compiler uses the .h-files while the linker uses the .c files. The linker needs to know which C files are included. The error comes form the linker not getting the compiled .c-files.
-
M-Chen-3 over 3 yearsPlease explain how this relates to the question and how it works.
-
Admin over 3 years@m-chen-3 You can find set of examples at Dislin examples. Just click one of plot tutorial then select your language (C).
-
Chef Gladiator over 2 years@MartinJohansen are you the author?
-
Martin Johansen over 2 years@ChefGladiator, yes, I am the author.