How do I show the same matplotlib figure several times in a single IPython notebook?
Just call myFigure
in any cell after myFigure
was assigned in cell before.
For example in cell 1:
In [1]:
myFigure, myAx = plt.subplots()
myAx.plot([1,2,3])
In cell after that:
In [2]:
myFigure
This will show myFigure
Assuming that you're using the default matplotlib backend (%matplotlib inline
) in Jupyter Notebook.
Then, to show a plot in the notebook, you have to display the figure. There are 3 ways to do that.
Any just-created non-closed plots (see also this answer for how to close the plot) will be automatically displayed when the cell finishes running. This happened when you run the first cell.
Put
fig
(amatplotlib.figure.Figure
instance) at the end of any cell.This method is already mentioned in the other existing answer, but note that it must be at the last line, and must not have a trailing
;
. (this is the same for every object types. If you execute a cell with content1
it will show1
in the output, but1;
will not show anything)Use
IPython.display.display(fig)
.
Note that print(fig)
will not work, because this will only send a string representation of the figure to sys.stdout
(so something like <Figure>
is displayed in the notebook).