7

Right now I am passing a file to gnuplot via a a pipe in c, something like this:

 fprintf(gnuplotPipe, "plot \"data-file.dat\" using 1:2\n"); 

Is there any way that I don't have to write the data to a file and then pass it to gnuplot, so for example somehow pass an array or a stream to gnuplot, so I can skip the writing to a file process and then deleting the file? Any help would be appreciated.

2 Answers 2

4

Sure you can do that. An example of a terminal command that plots two points connected by a line is:

echo "plot \"< echo -e '4 5\n 3 2'\" w lp pt 2" | gnuplot

If you want to have postscript output you can add the following:

echo "set terminal postscript; plot \"< echo -e '4 5\n 3 2'\" w lp pt 2" | gnuplot &> out.ps

If you now use system() or popen() (to catch the output stream) the rest should be straightforward.

Edit: There seems to exist some C(++) - Gnuplot interfaces. Check out this website which gives a great overview over C, C++, Python and Fortran wrappers. I'm not sure whether they are up to date and work with the latest Gnuplot versions but if not adapting shouldn't be that difficult.

4
  • Thanks for the reply, I am not sure though why I would need to catch the output, maybe I am not seeing what you are saying, the graph appears automatically after the plot command.
    – mihajlv
    Mar 1, 2012 at 22:08
  • Optional in case you plan to do more than just looking at the data. For simple plotting system will be fine.
    – Azrael3000
    Mar 1, 2012 at 22:21
  • sounds good, I'll try your answer, I'll have to convert the array to a huge char array guessing from your answers so I can pass it gnuplot, unless I can somehow directly pass the array (of doubles) itself to gnuplot?
    – mihajlv
    Mar 1, 2012 at 22:35
  • I updated my answer a little, providing a link that turned up after a quick search.
    – Azrael3000
    Mar 2, 2012 at 7:19
1

Gnuplot understands the filename '-' to mean forthcoming data. So to cut and paste from what I'm working on at the moment:

FILE *f = popen("gnuplot", "w");

fprintf(f, "set term png; "
"set out 'beforeafter.png'\n"
"set xlabel 'before'\n"
"set ylabel 'after'\n"
"plot '-'\n");

//Am using Apophenia in my project, so use its print function to print the data
apop_data_print(dataset, .output_pipe=f);

//or use fprintf directly
for (int i=0; i< data_len; i++)
  fprintf(f, "%g %g\n", data_x[i], data_y[i]);

fclose(f);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.