# How to save plots early and plot them together on same axes later?

**URL:** https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467
**Category:** New to Julia
**Tags:** plotting
**Created:** [November 19, 2020, 11:35pm UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467 "2020-11-19T23:35:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 19, 2020, 11:35pm UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467/1 "2020-11-19T23:35:39Z")

</div>

Hi again good folks, I’ve got another question about something that seems simple, but I can’t figure out how to do it after much searching. I’m trying to have a delayed-effect version of what you normally get with commands like:

```julia
plot(stuff1...)
plot!(stuff2...)
plot!(stuff3...)

```

The problem is that each piece of data, “stuffN”, is calculated in a different branch of a `For` loop, and each mass of data takes up too much RAM to hold them all in memory at the same time. All I can do is save the plots and try to put them together later, when the `For` loop is done. So consider a situation where I’ve saved an array of plots…

```julia
plot1 = plot(stuff1...)
plot2 = plot(stuff2...) 
plot3 = plot(stuff3...) #and so on...
plotArray = [plot1, plot2, plot3, plot4, plot5, . . .]

```

Then how to get all of these plots put together on the SAME axes, in the SAME plot?

Nothing works so far. Using `layout` doesn’t work, because the plots are put on different axes in different picture blocks. Trying `display!(plot1, plot2, . . . )` produces an error. This also fails, because it doesn’t stop the screen from refreshing for each plot:

```julia
plot(plot1)
plot!(plot2) 
# and so on...

```

Also, changing the above definitions to things like the commands below produces spurious plot-combination results, depending undesirably on the order in which they first appeared on the screen:

```julia
plot1 = plot(stuff1...)
plot2 = plot!(stuff2...) 
plot3 = plot!(stuff3...) #and so on...
plotArray = [plot1, plot2, plot3, plot4, plot5, . . .]

```

Any ideas…? I’m fresh out.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 19, 2020, 11:43pm UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467/2 "2020-11-19T23:43:50Z")

</div>

The `plot!` command tries to plot onto whatever the most recent figure was, which is (as you’ve discovered) not a particularly reliable way to do things. Instead, you can use the returned value from one `plot` to reliably add to the _same_ plot later:

```julia
julia> plt1 = plot([1, 2, 3])

julia> plt2 = plot([4, 5, 6])

julia> plot!(plt1, [7, 8, 9])

```

In this case, only `[1, 2, 3]` and `[7, 8, 9]` show up in the same plot because we have explicitly told `plot!` which figure to use. Perhaps you can hold a single plot outside of the `for` loop and then `plot!()` into that plot at each iteration? Something like:

```julia
julia> plt = plot()

julia> for i in 1:10
         plot!(plt, i .* (1:5))
       end

julia> display(plt)

```

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 19, 2020, 11:56pm UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467/3 "2020-11-19T23:56:11Z")

</div>

Thanks rdeits, that works! During each iteration of the `For` loop, I suppose I can update `plotTotal` using the previous `plotTotal` plus the new `plotNew` during each iteration.

One complication, if I may ask a follow-up question: the plots are actually 2D contour plots of the form:

```julia
p1 = contour(xCommonData, y1Data, F1ofXYdata)
p2 = contour(xCommonData, y2Data, F2ofXYdata) #and so on...

```

What would be the correct syntax to add them together?  
(For the above 1D cases, I tried things like `plot!(plot1, xCommonData, y2Data)`, and `plot!(plot1, plot(xCommonData, y2Data))`, and both produced errors.)

Thanks again for any info…!

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 20, 2020, 12:21am UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467/4 "2020-11-20T00:21:52Z")

</div>

Oops, `plot!(plot1, (xCommonData, y2Data))` actually did work to combine them, I suppose something similar will work to combine the 2D contour plats. Thanks again…

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [November 25, 2020, 6:18am UTC](https://discourse.julialang.org/t/how-to-save-plots-early-and-plot-them-together-on-same-axes-later/50467/5 "2020-11-25T06:18:37Z")

</div>

I have another question about this issue, since the `plot!` command seems to trash the original contour plot so I can’t use it anymore. Even the following commands don’t let me use the original contour plot again:

```julia
TempPlot = OriginalContourPlot;
CombinedPlot = contour(TempPlot); plot!(xDataArray, yDataArray); 
               display(CombinedPlot)

```

Once I run these commands, I cannot do them again (for example, with different plot display parameters); and I can’t even run `contour(OriginalContourPlot)` again. I get this error message:

```julia
MethodError: no method matching length(::Nothing)

```

Any ideas on how to do this combined contour-plus-line plot without trashing the original contour plot?? Thanks!
