# How to save a plot structure

**URL:** https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694
**Category:** General Usage
**Tags:** plots, vegalite
**Created:** [September 12, 2019, 3:26pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694 "2019-09-12T15:26:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [September 12, 2019, 3:26pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694/1 "2019-09-12T15:26:23Z")

</div>

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:

```julia
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 ?

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [September 12, 2019, 4:14pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694/2 "2019-09-12T16:14:22Z")

</div>

That works nicely with [VegaLite.jl](https://github.com/queryverse/VegaLite.jl):

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

```

New session:

```julia
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).

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [September 12, 2019, 5:11pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694/3 "2019-09-12T17:11:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [September 12, 2019, 8:04pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694/4 "2019-09-12T20:04:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)
#### Post date: [September 18, 2019, 11:21pm UTC](https://discourse.julialang.org/t/how-to-save-a-plot-structure/28694/5 "2019-09-18T23:21:04Z")

</div>

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:  
 → [Backends · Plots](http://docs.juliaplots.org/latest/backends/#hdf5-hdf5-plots)

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

```julia
#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")
display(pread)

```

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