How to prevent plot! from over-writing earlier saved plot

Hi, I’ve asked this as part of an earlier question, but no resolution yet.

In my program, I have this contour plot extracted from data after many hours of execution,
and I have to add new data on top of the contour plot (in the same window, on the same axes). So it was suggested I do this:

plot!(SavedContourPlot, (NewYdata, NewXdata))

and that works… BUT, it over-writes SavedContourPlot, which ruins it, since I must use that original contour plot again later with other data, but cannot waste the time to re-calculate it. These commands also don’t help:

HoldTheOldPlot = SavedContourPlot
NewPlot = plot!(SavedContourPlot, (NewYdata, NewXdata)); display(NewPlot)

because now HoldTheOldPlot , NewPlot , and SavedContourPlot ALL get changed in the same way, getting the new data added to the plots.

Is there any way that I can use the plot! command in the way shown above, which adds the new data to the old plots in the way that I need it, but ALSO keep an unchanged copy of the old contour plot somehow? Thanks…!

Can you do something like this?

OldXData,OldYData = CalculateData(a,b,c) #this process takes ages
SavedContourPlot = plot(OldXData,OldYData)
NewContourPlot = plot(OldXData,OldYData) #do twice the plot, this shouldn't recompute the data, you end up with two different objects
plot!(NewContourPlot, (NewYdata, NewXdata)); display(NewContourPlot) #Modify only one of the plot objects

Another option is to do what you were trying but using the deepcopy function:

HoldTheOldPlot = deepcopy(SavedContourPlot)
NewPlot = plot!(SavedContourPlot, (NewYdata, NewXdata)); display(NewPlot)

This will work because you are actually copying the plot object and associating it with the binding HoldTheOldPlot, whereas in your version, you were only pointing said binding to the same object.

You can read more about the deepcopy function by doing ?deepcopy in the REPL

Yes, deepcopy works, thank you!

(The first solution wouldn’t have worked, since I need to make copies of the contour plot on the fly, after the source data is already gone.)