Strange behavior of plot---plot successful only if it is the last line of the code

I am using vscode in anaconda. The following code

using Plots

a = 3+4
plot([1,2,3],[2,4,8])

can produce a figure. But this code cannot

using Plots

plot([1,2,3],[2,4,8])
a = 3+4

It seems that the plot is displayed only if it is the last line of the code.

using Plots

p=plot([1,2,3],[2,4,8])
a = 3+4
p
1 Like

It works!

But what if I have more than two figures to plot?

using Plots

p=plot([1,2,3],[2,4,8])
p2=plot([1,2,3],[2,4,-8])
a = 3+4

p
p2

Only the last figure is plotted.

1 Like

When you input something to the REPL (including a file), the result of the last line is shown. It is shown via the display function. So

using Plots

display(plot([1,2,3],[2,4,8]))
plot([1,2,3],[2,4,8]) |> display # same as above, but I think |> looks nicer here
a = 3+4

will display the plot twice and then display 7 since it’s the final line.

(Unless, of course, the second plot overwrites the first. Whether that is possible depends on the specifics of the plotting package you use and doesn’t seem to be a problem that you’ve indicated so far.)

1 Like

With Plots.jl you have two options: (i) plot(p, p2) , (ii) create two separate windows.
The latter works for a number of backends - see this thread.

1 Like