How to save a plot structure

I’d like to save a plot but not as an image file, basically I want to be able to save a Plot p, end my julia session, open a new one, load Plot p and then edit it with new series for example.

I tried to use BSON like this:

p = plot(rand(5))
BSON.@save "myplot.bson" p 
#restart julia session
BSON.@load "myplot.bson" p
> UndefVarError: PyCall not defined

I tried adding and using the PyCall package but then another undefvarerror shows up.

Is there a convenient way to do this ?

That works nicely with VegaLite.jl:

data |> @vlplot(:point, x=:colA, y=:colB) |> save("file.vegalite", include_data=false)

New session:

new_data |> load("file.vegalite")

In the first step you are saving a vega-lite JSON spec that describes the structure of the plot. In the second step, you are loading that structure again, but then you pipe new data into that old structure, and that creates a new plot with the new data but the old structure.

The include_data=false is optional, you can also save the plot with the data (and then the file can for example be opened and viewed directly in JuptyerLab, which has native vega-lite support built in).

Thank you, I’ll consider but I was hoping for something compatible with Plots and its many backends.

I think you can use the HDF5 saving functionality to do that?

Yes @HenriDeh:
It sounds to me like what you are looking for is exactly what the “hdf5-plots” feature of Plots.jl was designed to do.

You can find a simple example in the documentation here:

But just to keep from making you search, I will copy it here:

#First, generate your plot using Plots.jl:
using Plots
hdf5() #Select HDF5-Plots "backend"
p = plot(...) #Construct plot as usual

#Then, write to .hdf5 file:
Plots.hdf5plot_write(p, "plotsave.hdf5")

#After you re-open a new Julia session, you can re-read the .hdf5 plot:
using Plots
pyplot() #Must first select some backend
pread = Plots.hdf5plot_read("plotsave.hdf5")

If you cannot seem to get this working, please let me know.