Plot an IPython Notebook figure inline with fig.show()?

房东的猫 提交于 2019-12-22 07:37:15

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/15480379/plot-an-ipython-notebook-figure-inline-with-fig-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!