14

I am trying to use replot with png terminal in Gnuplot.

If I do the following I get two plots on one graph without any problem:

plot sin(x)/x
replot sin(x)

Now if do the same for a png terminal type the resulting png file only contains the first plot.

set terminal png
set output 'file.png'
plot sin(x)/x
replot sin(x)

Am I missing something at the end to get the second plot in my png file?

1 Answer 1

18

This is actually a very good question, and the behavior here is terminal dependent. Some terminals (e.g. postscript) will give you a new page for each replot. You have a couple of solutions...

First Option: You can make your plot prior to setting the terminal/output and then replot again after you set the terminal/output:

plot sin(x)/x
replot sin(x)
set terminal png
set output 'file.png
replot

This option is sometimes convenient if you want to plot the same thing in multiple terminals, but I rarely use it for anything else.

Second (better) Option: You can pack multiple plots into one command separating each with a comma.

set terminal png
set output 'file.png'
plot sin(x)/x, sin(x)

I very much prefer the second way -- when in a multiplot environment, this is the only way to put multiple graphs on the same plot. If you have very long functions to plot, you can break the line with gnuplot's line continuation (\ at the end of the line -- Nothing is allowed after the \, not even whitespace)

plot sin(x)/x with lines linecolor rgb "blue" linetype 7 lineweight 4, \
     sin(x),                                                           \
     cos(x)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. These commands are to be sent from a script. I can use the first option and do 'set terminal unknown' before to avoid the window poping up. The second option makes it slightly complicated for what I need. I really need the 'replot' way.
@Noel -- No problem. Glad to help. One other thing is that the second solution will be slightly more efficient. And I'm super happy to find out that you learned about the unknown terminal. It's a nifty corner of gnuplot most people don't know about. Good luck!

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.