I use IPython notebooks with matplotlib.pyplot and I often create a plot that requires a fairly large block of code to generate. I would then like to save the object and use that exact same figure/axes pair to incorporate in another plot later on.
For example, suppose I have some pairs of x-y data for a scatter plot. I'd like to show the points and then several cells later--potentially with other calls to pyplot to make other, unrelated figures--I would like to show that figure again so that I could plot over it with a regression line or some other graphics.
In the picture I've attached below, I have a short notebook; I want the figure from cell #2 to be drawn in cell #3 without calling pyplot.scatter again.
Essentially, I want to show the figure again without repeating all the code. What are my options for doing this? I have been unable to accomplish this with calls to show() or draw() or by setting the current figure object in the cell to be my saved figure object. Any advice is welcome. Thanks!
P.S. I know that if I reuse the figure and plot over it, the object will change and so the state of the fig object may not match the plot that was drawn in a previous IPython cell. That's okay for my work.
Just call
myFigure
in any cell aftermyFigure
was assigned in cell before.For example in cell 1:
In cell after that:
This will show
myFigure