当要求绘制第二个图表时,Python Matplotlib 挂起(关闭第一个图表窗口后)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1219394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:43:23  来源:igfitidea点击:

Python Matplotlib hangs when asked to plot a second chart (after closing first chart window)

pythonmatplotlib

提问by Thomas Browne

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:

我正在运行以下代码以使用 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:

正如预期的那样,我得到了人们所期望的图表,在一个弹出的新窗口中,包含一条非常简单的蓝线,在 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()

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).

不过,这一次,我得到了一个窗框,里面什么都没有(只有框架,没有白色背景),整个 bang 拍摄都挂了。我必须“结束任务”,python 解释器被系统终止,我得到一个命令提示符。mac 上的类似行为(除了它确实先绘制图形,然后再挂起)。

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.

所以不知何故 Python 和/或 matplotlib 不希望我手动关闭窗口。有人知道发生了什么以及我应该做什么吗?我想做的是在解释器中处理不同的情节,显然这种行为无济于事。我知道我可以使用“Ipython -pylab”,但为了学习,我想了解上述错误。

Thanks.

谢谢。

采纳答案by dalloliogm

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).

显然,这是由 tkinter 后端中的错误引起的。参见,例如https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/313834。它正在处理中......如果你可以回归到一个稍微旧的 tkinter 库,那应该是暂时的解决方法(几周前我遇到了同样的事情,这是我唯一的希望)。

回答by Rich

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.

聚会晚了三个月,但我在 matlibplot 文档中找到了使用 draw() 而不是 show() 的建议;前者显然只是对当前情节进行渲染,而后者则启动所有交互工具,这似乎是问题的开始。

It's not terribly prominently placed in the documentation, but here's the link: http://matplotlib.sourceforge.net/faq/howto_faq.html#use-show

它在文档中并没有特别显眼,但这是链接: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.

对于它的价值,我已经尝试过 pylab.show() 并且遇到了与您完全相同的问题,而 pylab.draw() 如果我只想查看输出似乎可以正常工作。

回答by dalloliogm

Have you tried to use ipython instead of the standard python interpreter?

您是否尝试过使用 ipython 而不是标准的 Python 解释器?

You can install ipython with the following command:

您可以使用以下命令安装 ipython:

easy_install ipython

and then, ipython has a specific mode to be ran with pylab, called -pylab:

然后,ipython 有一个特定的模式可以与 pylab 一起运行,称为 -pylab:

ipython -pylab

In[1]: ...

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.

我认为大多数人都使用这个解决方案来用 python 绘制图形,它是一个类似于 R/Matlab、完成等的命令行......它为每个绘图运行一个单独的线程,所以它不应该有你描述的问题。

回答by Nope

did you try:

你试过了吗:

plt.close()

to make sure you closed the plot object?

确保您关闭了绘图对象?

回答by anonymus

As posted somewhere above:

正如上面某处张贴的那样:

Use plt.draw()for all your plots except the last one.

使用plt.draw()您的所有地块除了最后一个。

For your last plot, use plt.show()

对于您的最后一个情节,请使用 plt.show()

It's weird, but if you don't use plt.show()in the last one and try plt.draw()instead, you don't see any plots.

这很奇怪,但是如果您plt.show()在最后一个中不使用并尝试plt.draw()使用,您将看不到任何情节。

Good luck with this!

祝你好运!

回答by Yonatan Simson

I had this problem when using TkAgg as the backend. After using plt.close('all')my computer froze.

我在使用 TkAgg 作为后端时遇到了这个问题。使用后plt.close('all')我的电脑死机了。

The solution was to switch to a different backend. I now use Qt4Agg instead.

解决方案是切换到不同的后端。我现在使用 Qt4Agg 代替。

If you have Qt4Agg installedit is possible to switch backends by typing:

如果您安装Qt4Agg,则可以通过键入以下内容来切换后端:

plt.switch_backend('Qt4Agg')

before plotting data

在绘制数据之前