6
votes

I'm calling the inline mode for IPython Notebook using;

%pylab inline

And the following code plots a figure immediately at the cell;

fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])

However I would like to create the plot/axes etc. in one cell, and plot later using maybe;

fig.show()

How do I gain more control of the inline mode? If I don't use %pylab inline, it creates the plot in a seperate window which I don't want (and it usually freezes the window).

Versions;

Numpy: 1.7.0
Matplotlib: 1.2.1rc1
Python: 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)]
Pandas: 0.10.1
PyLab: 1.7.0
3
Have you tried with the display function of Ipython? it is the "ufficial" method, as far as I know - EnricoGiampieri
Is it that you don't want the figure generated, or you just don't want to look at the empty axes? - askewchan
In general, I want to create a figure in one cell, work on it say in another using it's handle, and hold off on displaying it until yet a different cell later on. - Marcus Jones
If either of the two answers posted solve you problem, please accept it (big check mark on the left). - tacaswell

3 Answers

5
votes

You might be looking for disabling autoclose figure :

InlineBackend options
---------------------
--InlineBackend.close_figures=<CBool>
    Default: True
Close all figures at the end of each cell.
When True, ensures that each cell starts with no active figures, but it also
means that one must keep track of references in order to edit or redraw
figures in subsequent cells. This mode is ideal for the notebook, where
residual plots from other cells might be surprising.
When False, one must call figure() to create new figures. This means that
gcf() and getfigs() can reference figures created in other cells, and the
active figure can continue to be edited with pylab/pyplot methods that
reference the current active figure. This mode facilitates iterative editing
of figures, and behaves most consistently with other matplotlib backends,
but figure barriers between cells must be explicit.

still, IPython will show the figure if the last line of a cell return a fig object, you can avoid that by ending it with a ; or add pass as the last line.

5
votes

So I guess what you want is this:

from matplotlib.backends.backend_agg import FigureCanvasAgg as fc
fig = Figure()
canvas = fc(fig)
ax = fig.add_subplot(1, 1, 1)
ax.plot(arange(10))

To display the plot in another cell simply use:

fig
1
votes

With newer and

  • Jupyter: 4.6
  • Jupyter notebook: 6.0
  • Matplotlib: 3.1
  • ipykernel: 5.1

all you really need is to create your figure with matplotlib.pyplot.Figure (in one cell) and then make that figure the cell value in another cell. For example

In cell [1]

%matplotlib inline 

In cell [2]

from matplotlib.pyplot import Figure
from numpy import arange 
from numpy.random import normal 

fig = Figure()
ax  = fig.add_subplot(111)

ax.plot(arange(10),normal(size=10),label='Data')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.legend();

and finally in cell [3]

fig

That should be enough. See the screenshot below

Screenshot

Note suggestions with matplotlib.pyplot.ioff() and similar does not work