How can I show the same matplotlib metric several times in the same IPython laptop?

I use IPython notebooks with matplotlib.pyplot, and I often create a plot that needs to generate a fairly large block of code. Then I would like to save the object and use the same pair of shapes / axes to later include in another plot.

For example, suppose I have an xy data pair for a scatter plot. I would like to show the points, and then a few cells later - potentially with other calls on pyplot, to make other, unrelated shapes - I would like to show this shape again so that I can draw a regression line or some other graphics above it.

In the picture that I gave below, I have a short notebook; I want the shape from cell # 2 to be drawn in cell # 3 without again calling pyplot.scatter.

Essentially, I want to show the shape again without repeating all the code. What are my options for this? I was not able to accomplish this using show () or draw () calls or by setting the current shape object in a cell as my saved shape object. Any advice is appreciated. Thanks!

PS I know that if I reuse a figure and the graph above it, the object will change, and therefore the state of the fig object may not correspond to the plot that was drawn in the previous IPython cell. This is good for my work.

IPython notebook example

+7
matplotlib ipython jupyter-notebook jupyter
source share
1 answer

Just call myFigure in any cell after myFigure been assigned to the cell before.

For example, in cell 1:

 In [1]: myFigure, myAx = plt.subplots() myAx.plot([1,2,3]) 

In the cell after that:

 In [2]: myFigure 

This will show myFigure

+10
source share

All Articles