juliaplots.jl

How to clean Plots (GR) without closing Julia environment


I'm debugging a script (which use Plots.jl with GKS QtTerm backend). So I run the script many times. When I run it from terminal like bash> julia pointPlacement.jl it takes for ages to initialize Julia and Plots.jl (this is one big inconvenience in comparison to python). Therefore I rather keep Julia open and run the script from within, like julia> include( "pointPlacement.jl" )

grid = [ [ix*0.01 iy*0.01] for ix=1:100, iy=1:100 ]
grid = vcat(ps...)

centers = hexGrid( 2, 0.2 )

using Plots
display(scatter!( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))

The problem is that the plots accumulate. This is after 9 runs. There should be just 2 datasets, not 18:

enter image description here

I want to close (kill,destroy) them

If I remove ! like this, it helps

display(scatter( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))

but still, I worry that some junk (previous figures) stay allocated in memory and Julia will crash after I run the script 100x. Therefore I would like to call some function like clear(),flush(),closeAll() ... or something ... everytime I run the script


Solution

  • Removing the ! has the effect that you want - the plot is gone if you call scatter again and it doesn't live somewhere in the background.

    If you want you can store the plot in a variable and overwrite it "to be safe", i.e.

    p = scatter(...)
    scatter!(p, ...)
    

    , where ... are your plotting arguments. This will explicitly overwrite p on every include.