Reputation: 10506
When I write a C++ program, including the use of GNU-Plot via pipe, the plot is rendered however, all x11 interactivity is lacking, for example, I have taken the following code based HERE
int main()
{
FILE *pipe = popen("gnuplot -persist","w");
fprintf(pipe, "set samples 40\n");
fprintf(pipe, "set isosamples 40\n");
fprintf(pipe, "set hidden3d\n");
fprintf(pipe, "set xrange [-8.000:8.000]\n");
fprintf(pipe, "set yrange [-8.000:8.000]\n");
fprintf(pipe, "set zrange [-2.000:2.000]\n");
fprintf(pipe, "set terminal x11\n");
fprintf(pipe, "set title 'We are plotting from C'\n");
fprintf(pipe, "set xlabel 'Label X'\n");
fprintf(pipe, "set ylabel 'Label Y'\n");
fprintf(pipe, "splot cos(x)+cos(y)\n");
pclose(pipe);
return 0;
}
However, If I open a command line, run gnuplot, and manually type in all the commands, the full interactivity exists, ie, zoom, rotate etc...
Does someone know how I can get the interactivity working when the GNU-Plot is called via C++ program?
Upvotes: 0
Views: 958
Reputation: 44043
Interaction with gnuplot is only possible while the gnuplot main process is running. After you close the pipe, the main gnuplot process quits, and the gnuplot_x11 process it leaves behind does not handle input anymore.
The solution is to keep the pipe open and only close it when you don't want to use the plot anymore. You can try this out with the following changes:
#include <stdio.h>
int main()
{
FILE *pipe = popen("gnuplot -persist","w");
fprintf(pipe, "set samples 40\n");
fprintf(pipe, "set isosamples 40\n");
fprintf(pipe, "set hidden3d\n");
fprintf(pipe, "set xrange [-8.000:8.000]\n");
fprintf(pipe, "set yrange [-8.000:8.000]\n");
fprintf(pipe, "set zrange [-2.000:2.000]\n");
fprintf(pipe, "set terminal x11\n");
fprintf(pipe, "set title 'We are plotting from C'\n");
fprintf(pipe, "set xlabel 'Label X'\n");
fprintf(pipe, "set ylabel 'Label Y'\n");
fprintf(pipe, "splot cos(x)+cos(y)\n");
fflush(pipe); // force the input down the pipe, so gnuplot
// handles the commands right now.
getchar(); // wait for user input (to keep pipe open)
pclose(pipe);
return 0;
}
With this, the plot in the window can be handled until you press enter in the console where your program runs (then the program closes the pipe, gnuplot quits, and input handling stops).
Upvotes: 4