0

I want to print some data in a c++ application that I am doing using gnuplot. The point is that I want a png output. When I use the terminal it just works perfectly, so i get what i want but when i do it trough my application i dont know why is not generating the output.png. Here an example what i do:

//---GNUplot

   FILE *pipe;

   pipe = popen("/usr/local/bin/gnuplot --persist", "w");

   if (pipe != NULL){

     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 png\n");
     fprintf(pipe, "set output 'jose.png'\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");

     pclose(pipe);
   }

//---end

Any clue about what I am doing bad??

Many thanks in advance

Jose

1
  • Any errors you encounter? Commented Mar 10, 2016 at 20:37

1 Answer 1

3

Looking at what you included above, you have not specified a plot command. Gnuplot will not generate anything until it encounters a plot command. Just specifying labels and such will not cause any output.

You need to add something like

fprintf(pipe, "plot sin(x)\n");

or whatever you want to plot. If you are feeding it data you will provide individual lines of data followed by an 'e' to tell it to stop.

fprintf(pipe, "plot '-' u 1:2:3\n");
fprintf(pipe, "1 2 3\n");
fprintf(pipe, "4 5 6\n");
fprintf(pipe, "e\n");

It is also good practice to end with a blank set output command to flush the output buffer before you close the pipe:

fprintf(pipe, "set output\n");

Finally, you don't need the persist command when you initialize gnuplot. That command only is necessary if you are working with an interactive terminal and want it to stay open even after the calling process ends.


Additionally, it is sometimes necessary to make sure that the output directory is as expected. Specifying the full path to the output file can help in this case, using set output 'full/path/to/file.png' or even set output './file.png'.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Mathew, first thanks for your quick answer. You are right i forgot this line of code: fprintf(pipe, "splot cos(x)+cos(y)\n"); and I put --persist because I wanted to see the output when I am not using set terminal png... I added frprintf(pipe, "set output\n") but still is not generating the png
@JoseGarcia-Uceda If you do have the splot command, please edit it into your post so that it is complete with what you are running. Are you certain that the file is not just being generated in a different location? Try modifying the set output 'jose.png' command to use a complete path and see if that works. Also try it without the persist option and see if that works. I doubt that has anything to do with the problem, but it is worth making sure.
@JoseGarcia-Uceda Did you add the blank set output just before you closed the pipe and after the plot command?
SOLVED! it was the path as you say. I just put the absolute path and I got my beautiful chart :D many many thanks!
@JoseGarcia-Uceda I edited the answer to put that into it, so that the answer isn't buried in the comments. You may accept my answer, if you like. You are not obligated to, but it awards both me and you some reputation, and marks the question as answered.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.