Python 2.7 将IPython笔记本图形与fig.show()内联绘制?

Python 2.7 将IPython笔记本图形与fig.show()内联绘制?,python-2.7,matplotlib,ipython,ipython-notebook,jupyter,Python 2.7,Matplotlib,Ipython,Ipython Notebook,Jupyter,我正在使用调用IPython笔记本的内联模式 %pylab inline 下面的代码在单元格处立即绘制一个图形 fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) 但是,我想在一个单元格中创建绘图/轴等,稍后使用maybe进行绘图 fig.show() 如何获得对内联模式的更多控制?如果我不使用%pylab inline,它会在我不想要的单独窗口中创建绘图(通常会冻结窗口) 版本 Numpy: 1.7.0 Matplotlib: 1.2

我正在使用调用IPython笔记本的内联模式

%pylab inline
下面的代码在单元格处立即绘制一个图形

fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
但是,我想在一个单元格中创建绘图/轴等,稍后使用maybe进行绘图

fig.show()
如何获得对内联模式的更多控制?如果我不使用%pylab inline,它会在我不想要的单独窗口中创建绘图(通常会冻结窗口)

版本

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

您可能正在寻找禁用自动关闭的功能图:

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.
InlineBackend选项
---------------------
--InlineBackend.close\u数字=
默认值:True
关闭每个单元格末尾的所有图形。
如果为True,则确保每个单元格开始时没有活动图形,但
意味着必须跟踪引用才能编辑或重画
随后单元格中的数字。此模式非常适合笔记本电脑,其中
来自其他细胞的残留图可能令人惊讶。
如果为False,则必须调用figure()来创建新图形。这意味着
gcf()和getfigs()可以引用在其他单元格中创建的图形
可以使用pylab/pyplot方法继续编辑活动地物
参考当前活动图形。此模式有助于迭代编辑
与其他matplotlib后端的行为最为一致,
但细胞之间的屏障必须明确。

尽管如此,如果单元格的最后一行返回fig对象,IPython仍将显示该图,您可以通过以
结尾来避免此情况
或添加
pass
作为最后一行。

所以我猜您想要的是:

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))
要在另一个单元格中显示绘图,只需使用:

fig
更新和

  • 朱比特:4.6
  • Jupyter笔记本:6.0
  • Matplotlib:3.1
  • ipykernel:5.1
您真正需要的是使用
matplotlib.pyplot.figure
(在一个单元格中)创建图形,然后将该图形作为另一个单元格中的单元格值。比如说

单元格中的
[1]

单元格中的
[2]

最后在单元格中
[3]

这应该足够了。请参见下面的屏幕截图


注意
matplotlib.pyplot.ioff()
和类似的建议不起作用

您尝试过Ipython的显示功能吗?这是“ufficial”方法,据我所知,是你不想生成图形,还是你只是不想看到空轴?一般来说,我想在一个单元格中创建一个图形,在另一个单元格中使用它的句柄进行处理,并推迟显示,直到稍后出现另一个单元格。如果发布的两个答案中的任何一个解决了问题,请接受它(左侧的大复选标记)。c.InlineBackend.close\u figures=False现在在\myIPythonDir\profile\myHomeProfile\ipython\u notebook\u config.py中设置。axes=fig.add_axes([0,0,1,1])cmd仍然会在单元格后显示一个绘图,即使使用分号和pass。有没有办法查看当前选项以检查是否确实设置了此标志?
%config InlineBackend
应该告诉您不同的配置值。这一个几乎可以工作!我只是无法在中间单元格中得到令人惊讶的数字,即:;或将matplotlib interactive密码集设置为false
matplotlib.ioff()
IIRC.Thanx获取答案。这也意味着显示器(图)将完成此操作。
%matplotlib inline 
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();
fig