How to make plot window stay open when running a .jl script

I write a simple script, plotex1.jl to produce a plot;

using Plots
pyplot() # Switch to using the PyPlot.jl backend
plot1 = plot(rand(10,10),linewidth=2,title=“Plot 1”)
display(plot1)

The I run the script from my terminal
$ julia plotex.jl

I see the plot flash for a split second and then it disappears. How can I make the plot window stay open?

(If I run the code from REPL, the last line is not needed and the plot window shows and stays open)

after the plot, add
gui()

alternatively, do
display(plot1)

First, please quote your code: this makes it easier to run your examples and help you.


As for your question: the problem is that you need Julia to stay alive for it to display the graph. There are two ways (that I know of):

  1. run your script like this:

    sh> julia -i plotex.jl
    

    this will make the REPL stay alive after having executed your file. You can look at your plot, and kill the REPL manually afterwards as usual, using for example Ctrl+d or exit().

  2. add readline() at the end of your script. This will make Julia wait until you press RET before it exits (and makes your plot disappear)

Thanks,

Here is some feedback.

Adding gui() or display(plot1) alone did not make the plot window stay open. Nor did readline() . Adding readline() just made the terminal hang until i hit RETURN.

However adding gui() and executing the script with
$julia -i plotex.jl did the trick.

I will post another question under a performance heading as well. When I type the code manually into REPL the plot shows immediately after the last line. When I run the code as a script from a terminal it takes 27 seconds for the plot to appear! Can anyone explain this?