Passing data array to gnuplot via pipe in c, not file

Now I am transferring the gnuplot file through a pipe in c, something like this:

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

Is there a way that I do not need to write data to a file and then transfer it to gnuplot, so, for example, somehow pass an array or stream to gnuplot, so I can skip writing to the process file and then delete the file? Any help would be appreciated.

+6
source share
2 answers

Of course you can do it. An example of a terminal command that displays two points connected by a line:

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

If you want to have a postscript, 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 are now using system () or popen () (to catch the output stream), the rest should be simple.

Edit: It seems there are some C (++) - Gnuplot interfaces. Check out this website , which gives an excellent overview of C, C ++, Python, and Fortran wrappers. I'm not sure that they are relevant and work with the latest versions of Gnuplot, but if you do not adapt, it should not be so difficult.

+4
source

Gnuplot understands the file name '-' to indicate upcoming data. So cut and paste from what I'm currently working on:

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

Source: https://habr.com/ru/post/909783/


All Articles