无法在matplotlib中重新绘制数字

时间:2017-10-09 07:05:04

标签: python matplotlib

继承人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
  • 关于我的最后一点,其他问题没有解释。好吧没有数字可以显示......这就解释了为什么没有数字输出。但这并没有回答这个问题。如何获得一个简单的功能界面,用户可以在其中修改现有的绘图并随意重新绘制它?

1 个答案:

答案 0 :(得分:0)

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