【问题标题】:Cannot re-plot figures in matplotlib无法在 matplotlib 中重新绘制图形
【发布时间】:2017-10-09 18:53:24
【问题描述】:

这是我在matplotlib.pyplot 中尝试使用jupyter qtconsole 作为我的解释器:

In [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3])
Out[2]: [<matplotlib.lines.Line2D at 0x7ab5710>]
In [3]: plt.show()
plt.plot([4,5,6])
Out[4]: [<matplotlib.lines.Line2D at 0x7d8d5c0>]
In[5]: plt.show()
In[6]: plt.figure(1).show()
c:\python35\lib\site-packages\matplotlib\figure.py:403: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "

In[3]In[5] 都成功输出了数据的内联图。 In[6] 是我重新绘制第二个数字的尝试。不幸的是,我收到一条关于非 GUI 后端的错误消息。

理想情况下,我想做的是首先绘制数据(例如使用脚本),然后使用解释器修改绘图,重新绘制它,看看我是否喜欢这些变化,再修改一些,重新绘制等等。甚至可以使用我上面的设置?

编辑

与假定的重复有两个主要区别:

  • 另一个问题中的 OP 正在使用已弃用的 pylab
  • 关于我的最后一点,其他问题没有任何解释。好的,所以没有要显示的数字......这解释了为什么没有输出数字。但这并不能回答问题。如何获得一个简单的功能界面,用户可以在其中修改现有的情节并随意重新绘制?

【问题讨论】:

标签: python matplotlib


【解决方案1】:

python 控制台或脚本中的行为肯定与 jupyter qt 控制台中的有些不同。对于 python 脚本this answer 是完全正确的:调用plt.show() 后没有图形可显示。
在 jupyter qt 控制台中,使用了不同的后端,默认情况下会自动绘制内联图形。这开启了使用面向对象的 API 并使用图形和坐标轴句柄的可能性。

这里,只需在单元格中声明图形句柄即可显示图形。

In [1]: import matplotlib.pyplot as plt

In [2]: fig, ax = plt.subplots()

In [3]: ax.plot([1,2,3])
Out[3]: [<matplotlib.lines.Line2D at 0xb34a908>]

In [4]: plt.show() # this shows the figure. 
# However, from now on, it cannot be shown again using `plt.show()`

# change something in the plot:
In [5]: ax.plot([2,3,1])
Out[5]: [<matplotlib.lines.Line2D at 0xb4862b0>]

In [6]: plt.show()  # this does not work, no figure shown

# However, just stating the figure instance will show the figure:
In [7]: fig
Out[7]: # image of the figure

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多