I have been trying to plot some things using GNUplot from a C program. I have just taken a code from an answer to this question for now: Making C code plot a graph automatically
This is my code:
#include <stdlib.h>
#include <stdio.h>
#define NUM_POINTS 5
void main()
{
double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = _popen ("gnuplot -persistent", "w");
fprintf(gnuplotPipe, "plot '-' \n");
int i;
for (int i = 0; i < NUM_POINTS; i++)
{
fprintf(gnuplotPipe, "%g %g\n", xvals[i], yvals[i]);
}
fprintf(gnuplotPipe, "e\n");
fflush(gnuplotPipe);
fclose(gnuplotPipe);
}
I am running this using Cygwin. The problem is that the plot appears (I see it flash very briefly.) but doesn't "persist" on the screen.
I have tried with popen instead of _popen. And tried using pause -1 as well. I'm not sure what is missing/wrong. Changing "persistent" in line 15 to "persist" doesn't work either. Any help will be appreciated.
Thanks in advance! :)