当要求绘制第二个图表时(关闭第一个图表窗口后),Python Matplotlib 挂起
奇怪的行为,我确信是我搞砸了,但我想了解到底发生了什么:
我正在运行以下代码,使用 matplotlib 创建一个非常简单的图形窗口:
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x0290B750>]
>>> plt.show()
正如预期的那样,我得到了图表一可以预期,在弹出的新窗口中,包含一条非常简单的蓝线,在 y 轴上从 1 到 3 又回到 1,其中 0、1、2 作为 x 轴点(仅作为示例)。 现在我关闭图形窗口(使用窗口右上角的十字按钮)。 这让我可以控制解释器,然后我重新开始,创建新的对象:
>>>
>>> fig1 = plt.figure()
>>> bx = fig1.add_subplot(111)
>>> bx.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x029E8210>]
>>> plt.show()
不过这一次,我得到了一个窗框,里面什么也没有(只有框架,没有白色背景),整个爆炸镜头都挂起来了。 我必须“结束任务”,python 解释器被系统终止,并且我得到命令提示符。 Mac 上的类似行为(除了它实际上首先绘制图表,然后再挂起)。
因此,Python 和/或 matplotlib 不希望我手动关闭窗口。 有人知道发生了什么事以及我应该做什么吗? 我想做的是在解释器中尝试不同的情节,显然这种行为没有帮助。 我知道我可以使用“Ipython -pylab”,但为了学习的利益,我想了解上述错误。
谢谢。
Weird behaviour, I'm sure it's me screwing up, but I'd like to get to the bottom of what's happening:
I am running the following code to create a very simple graph window using matplotlib:
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x0290B750>]
>>> plt.show()
and as expected I get the chart one would expect, in a new window that has popped up, containing a very simple blue line going from 1 to 3 back to 1 again on y axis, with 0, 1, 2 as the x axis points (just as example). Now I close the graph window (using cross button in the top right under windows). This gives me control to the interpreter, and I start again, creating new objects:
>>>
>>> fig1 = plt.figure()
>>> bx = fig1.add_subplot(111)
>>> bx.plot((1, 3, 1))
[<matplotlib.lines.Line2D object at 0x029E8210>]
>>> plt.show()
This time though, I get a window frame, with nothing in it (just the frame, no white background nothing), and the whole bang shoot hangs. I have to "end task", the python interpreter is terminated by the system and I get a command prompt back. Similar behaviour on a mac (except it does actually plot the graph first, before also hanging).
So somehow Python and/or matplotlib doesn't want me to close the window manually. Anybody know what's going on and what I should be doing? What I'd like to do is play around with different plots from within the interpreter, and obviously this behaviour doesn't help. I know I could use "Ipython -pylab" but in the interests of learning, I want to understand the above error.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
聚会迟到了三个月,但我在 matlibplot 文档中发现了使用 draw() 而不是 show() 的建议; 前者显然只是渲染当前的情节,而后者启动所有交互式工具,这似乎是问题开始的地方。
它在文档中的位置并不是非常显眼,但这是链接:
http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show
就其价值而言,我尝试过 pylab.show() 并遇到了与您完全相同的问题,而如果我只想查看输出,则 pylab.draw() 似乎工作正常。
Three months late to the party, but I found a suggestion in the matlibplot documentation to use draw() rather than show(); the former apparently just does a render of the current plot, while the latter starts up all the interactive tools, which is where the problems seem to start.
It's not terribly prominently placed in the documentation, but here's the link:
http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show
For what it's worth, I've tried pylab.show() and had exactly the same issue you did, while pylab.draw() seems to work fine if I just want to see the output.
显然,这是由 tkinter 后端的错误引起的。 请参见,例如 https://bugs.launchpad.net/ubuntu/ +source/matplotlib/+bug/313834 。 正在处理中...
如果您可以回归到稍旧的 tkinter 库,这应该是暂时的解决方法(几周前我遇到了同样的事情,这是我唯一的希望)。
Apparently, this is caused by a bug in the tkinter backend. See, e.g., https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/313834 . It's being worked on...
If you can regress to a slightly older tkinter library, that should be a workaround for the time-being (I ran into this same thing a couple of weeks ago, and that was my only hope).
您是否尝试过使用 ipython 而不是标准的 python 解释器?
您可以使用以下命令安装 ipython:
然后,ipython 有一个与 pylab 一起运行的特定模式,称为 -pylab:
我认为大多数人使用此解决方案来使用 python 绘制图形,它是一个类似于以下的命令行R/Matlab、完成等之一...并且它为每个图运行一个单独的线程,因此它不应该出现您所描述的问题。
Have you tried to use ipython instead of the standard python interpreter?
You can install ipython with the following command:
and then, ipython has a specific mode to be ran with pylab, called -pylab:
I think that most of the people use this solution to plot graphs with python, it is a command line similar to the one of R/Matlab, completition, etc... and it runs a separated thread for every plot so it shouldn't have the problem you have described.
您是否尝试过:
确保关闭了绘图对象?
did you try:
to make sure you closed the plot object?
正如上面发布的:
对除最后一个之外的所有绘图使用
plt.draw()
。对于你的最后一个绘图,使用
plt.show()
这很奇怪,但是如果你在最后一个绘图中不使用
plt.show()
并尝试plt .draw()
相反,您看不到任何绘图。祝你好运!
As posted somewhere above:
Use
plt.draw()
for all your plots except the last one.For your last plot, use
plt.show()
It's weird, but if you don't use
plt.show()
in the last one and tryplt.draw()
instead, you don't see any plots.Good luck with this!
我在使用 TkAgg 作为后端时遇到了这个问题。 使用
plt.close('all')
后我的电脑死机了。解决方案是切换到不同的后端。 我现在改用 Qt4Agg。
如果您安装了Qt4Agg,则可以通过键入以下内容来切换后端:
在绘制数据之前
I had this problem when using TkAgg as the backend. After using
plt.close('all')
my computer froze.The solution was to switch to a different backend. I now use Qt4Agg instead.
If you have Qt4Agg installed it is possible to switch backends by typing:
before plotting data