【发布时间】:2018-03-07 08:51:25
【问题描述】:
我运行以下代码,使用 matplotlib 面向对象的 API 绘制单行。
$ ipython --pylab
In [1]: fig = plt.figure() # An empty figure is shown in popped up window here
In [2]: ax = fig.add_subplot(1,1,1)
In [3]: ax.plot(arange(10)) # no figure is shown up here
Out[3]: [<matplotlib.lines.Line2D at 0x7fb134118400>]
In [4]: fig.show()
fig = plt.figure() 执行时,会弹出新窗口,并显示一个空图。通过ax.plot(arange(10))绘制后,没有自动显示图形,所以我尝试通过fig.show()显示图形,我得到了这个错误。
AttributeError Traceback (most recent call last)
<ipython-input-6-6ebedab27258> in <module>()
----> 1 fig.show()
~/.pyenv/versions/3.6.4/envs/analysis/lib/python3.6/site-packages/matplotlib/figure.py in show(self, warn)
409 if manager is not None:
410 try:
--> 411 manager.show()
412 return
413 except NonGuiException:
~/.pyenv/versions/3.6.4/envs/analysis/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py in show(self)
560 self.canvas.draw_idle()
561 # Raise the new window.
--> 562 self.canvas.manager.window.attributes('-topmost', 1)
563 self.canvas.manager.window.attributes('-topmost', 0)
564 self._shown = True
AttributeError: 'NoneType' object has no attribute 'attributes'
但是像这样一个丑陋的单线正确地显示了一个折线图。
plt.figure().add_subplot(1,1,1).plot(arange(10))
IPython shell 似乎无法多次显示同一个图形。如何避免这种行为?
【问题讨论】:
-
如果您处于交互模式(听起来像是),那么您不需要致电
show()。ax.plot()应该更新图 -
感谢您的评论。我还期望
ax.plot()显示更新后的数字,但事实并非如此。更新了问题以澄清这一点。
标签: python matplotlib ipython jupyter