Hi! Welcome to Julia’s discourse forum!
The problem in here is that you need to either return the plot object from the function, or to call display() before. This is done automatically when you call plot directly in the REPL (and not inside a function).
example:
using Plots; gr()
function plotsomething()
p1 = plot(rand(10))
1+1 # doing something else
return p1
end
or calling display inside
using Plots; gr()
function plotsomething()
p1 = plot(rand(10))
1+1 # doing something else
display(p1)
sleep(10)
end
You can read about it in the documentation, which says:
Plotting in Scripts
Now that you’re making useful plots, go ahead and add these plotting commands to a script. Now call the script… and the plot doesn’t show up? This is because Julia in interactive use calls
display
on every variable that is returned by a command without a;
. Thus in each case above, the interactive usage was automatically callingdisplay
on the returned plot objects.In a script, Julia does not do automatic displays (which is why
;
is not necessary). However, if we would like to display our plots in a script, this means we just need to add thedisplay
call. For example:display(plot(x, y))
If we have a plot object
p
, we can dodisplay(p)
at any time.