4

I am writing a gnuplot (5.2) script and I would like to only plot certain functions or data files if a flag is set. An example of this that works with gnuplot is:

if(flag==0){plot sin(x)}

if(flag==1){plot sin(x), cos(x)}

so that depending on the value of 'flag' I can make slightly different plots. However, doing it the above way means repeating the plot command for every setting for the flag and this quickly becomes laborious if I have multiple settings for my flag. Is there a way to use the 'if' statement inside a plot command? For example, something like:

plot sin(x), if(flag==1){cos(x)}

so that I can do everything with only one use of 'plot'.

3
  • What's the point to use plot once? You want the program to be short? Commented Sep 30, 2017 at 0:18
  • The actual script I am writing is considerably more complicated than the short example I have given and I don't want to have to repeat the plot command for each different flag. Commented Sep 30, 2017 at 0:31
  • The only solution I see is to define a function which decides to plot what based on flag value. This function can minimizes number of times that you use plot in your large code Commented Sep 30, 2017 at 1:21

2 Answers 2

2

Two ideas:

The first idea uses the ternary operator:

unset key
a = 1
plot sin(x), (a == 1 ? cos(x) : NaN)

Depending on your needs, key handling gets difficult.

The second idea builds the plot command depending on some flags:

# initial plot command which plots nothing, additional functions 
# can be appended with a comma
plot_command = "plot NaN notitle"

sin_flag = 1
cos_flag = 1

if (sin_flag == 1) {
   plot_command = plot_command.", sin(x)"
}  

if (cos_flag == 1) {
   plot_command = plot_command.", cos(x)"
}  

# print the final plot command (just for a check)
print plot_command

# execute the plot command
eval plot_command
Sign up to request clarification or add additional context in comments.

Comments

1

Make an empty plot window

plot "-" ps 0 notitle
0 0
e

(or sth similar) and then do your original scheme, but with "replot"

if(flagsin==1){replot sin(x)}
if(flagcos==1){replot cos(x)}

4 Comments

Okay, great, this seems to work for my purposes. Note than when making the initial empty plot you could just use 'plot NaN' as long as you have specified a yrange.
I guess the disadvantage of your solution is that 'replot' can be annoying to use within multiplot and also that for output vector graphics files I think replot can end up making multiple copies of the same line on top of one another.
@Mead "multiplot" yes. That can't be helped. But your latter point is definitely no true. "replot" always clears the canvas of everything before redoing the previous plots.
Or let's say it creates a new, empty plane with a white background that includes previous multiplot output) in the far.

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.