1
votes

Of 3 versions of replotting only 2 work as desired:

plot sin(x)
replot sin(x)/2
replot sin(x)/3
replot sin(x)/4

After each command the respective curve is added to the plot - as expected.

plot sin(x)
replot for [i=2:4] sin(x)/i

gives the same (final) result (4 curves in the plot), but

plot sin(x)
do for [i=2:4] {replot sin(x)/i}

results in a plot with only the 1st and the 4th curve, although 4 lines are written in the key. Is this a "feature" or (how) can I get 4 curves in the do-for-version (because I have to do some additional stuff within the do-clause)?

[gnuplot 5.2 (8), Ubuntu MATE 20.04]

3
You could plot all 4 curves like this plot for [i=1:4] sin(x)/i. What else do you have to do inbetween why you need a do for loop?theozh
Besides some other stuff (calculations using 'i') I want to - said very simplified - also draw cos(x)/i. Using "replot for [i=2:4] sin(x)/i ; replot for [i=2:4] cos(x)/i" yields the wrong order, because I want sin(x), cos(x), sin(x)/2, cos(x/2, ...SandwichX
actually with your last example, 3 curves of sin(x)/4 will be on top of each other. If you type show plot in the gnuplot console you will get last plot command was: plot sin(x), sin(x)/i, sin(x)/i, sin(x)/i and for the last replot command i=4. I guess that's the way replot works. It will not plot sin(x), sin(x)/2, sin(x)/3, sin(x)/4.theozh
if the order is important, e.g. with your alternating sin(x), cos(x) example, you could use: plot for [i=1:4] for [b=0:1] b?sin(x)/i:cos(x)/i title sprintf("%s(x)/%d",b?'sin':'cos',i). But maybe you edit the question and describe your final goal in more detail.theozh
"I guess that's the way replot works." - looks like that, yes. So I found a solution (which I will post in a few seconds) with creating a macro. (Although this is not so nice in my real problem as it looks in this simple example here.)SandwichX

3 Answers

2
votes

I think the syntax that expresses what you were originally trying to do is this:

do for [i=1:4] {
    # ... do some stuff ...  #
    plot for [j=1:i] sin(x)/j title sprintf("sin(x)/%d",j)
}

replot is really not what you want for this sort of thing.

1
votes

Additioal Way

There is also a way to use the eval command in loop, as follows.

plot sin(x), cos(x)
do for [i=2:4] {
  # some calculations involving i
  command = sprintf("replot sin(x)/%i, cos(x)/%i", i, i)
  eval command
  # maybe some more stuff involving i
}
0
votes

So a way to use replot inside a do-for-loop is creating a macro:

plot sin(x), cos(x)
txt = ""
do for [i=2:4] {
  # some calculations involving i
  txt = txt . "replot sin(x)/" . i . ", cos(x)/" . i . " ; "
  # maybe some more stuff involving i
}
@txt