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

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:

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…

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:

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:

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

Any ideas…? I’m fresh out.

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> 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> plt = plot()

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

julia> display(plt)

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:

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…!

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…

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:

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:

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!