I use linux,C++. I want to save output from gnuplot. How can I do it using c++? I tried below code. It generates a png file.But it hasn't plot point. I want to do two task
- show a graph on runtime
- save the graph as gif when the program is finish.
How can I do them?
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:]\n");
fprintf(pipe, "set terminal png\n");
fprintf(pipe, "set output 'b.png'\n");
int b = 5;int a;
// to make 10 points
std::vector<int> x (10, 0.0); // x values
std::vector<int> y (10, 0.0); // y values
for (a=0;a<5;a++) // 10 plots
{
x[a] = a;
y[a] = 2*a;// some function of a
fprintf(pipe,"plot '-'\n");
// 1 additional data point per plot
for (int ii = 0; ii <= a; ii++) {
fprintf(pipe, "%d %d\n", x[ii], y[ii]); // plot `a` points
}
fprintf(pipe,"e\n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}