无法手动关闭Matplotlib图窗口 [英] Cannot manually close matplotlib plot window

查看:27
本文介绍了无法手动关闭Matplotlib图窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac OS X Yosemite上运行Python v2.7和matplotlib v1.5.0.直到最近,我都能够在交互式解释器中运行我的脚本,显示一个情节,然后手动关闭窗口.例如

I'm running Python v2.7 and matplotlib v1.5.0 on Mac OS X Yosemite. Up to recently, I was able to run my script in the interactive interpreter, show a plot, and then manually kill the window. For example

import numpy as np
x = np.arange(1,10)
y = np.arange(1,10)
plt.plot(x,y)
plt.show()

这过去工作正常.窗口将关闭,我将返回到提示.但是,现在当我按下红色的X关闭窗口时,该窗口保持打开状态.我的命令提示符返回并且可以正常工作,但是窗口被卡住了,我看到了旋转的沙滩球.

This used to work fine. The window would close and I'd return to the prompt. However, now when I hit the red X to close the window, the window remains open. My command prompt returns and works fine, but the window is stuck and I see the spinning beach ball over it.

(请注意,光标未出现在图像中,但您可以看到红色 X 变灰,因为我点击了关闭但它卡在打开状态).

(Note that the cursor doesn't appear in the image, but you can see the red X is greyed out because I've hit close but it is stuck open).

直到我第二次运行脚本时第一个窗口才关闭,但随后我被第二个打开的情节卡住了同样的问题.我有点困惑,因为这只是最近发生的.到目前为止,窗户会关闭得很好.

It's not until I run the script a second time that the first window closes, but then I'm stuck with a second open plot with the same problem. I'm a bit confused because this only happened recently. Up till now, the window would close fine.

这不是什么大问题,因为 Python 仍在运行,我仍然可以制作新图,但我很好奇为什么窗口会突然打开.有什么建议吗?

It's not a huge issue, because Python still runs and I can still make new plots, but I'm curious as to why the window would all of a sudden stick open. Any advice?

更新

我通过切换交互式后端解决了这个问题.Qt4Agg或TkAgg作为交互式后端都可以解决此问题.但是问题仍然是为什么macosx和CocoaAgg后端会显示这种行为.

I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue. But the question remains why the macosx and CocoaAgg backends show this behavior.

推荐答案

对于永久解决方案(我宁愿不必在每次打开ipython时都切换后端),您可以修改matplotlibrc.

For a permanent solution (I'd rather not have to switch backends every time I open ipython) you can modify matplotlibrc.

有各种 matplotlibrc 文件可以更改(即仅针对本地目录或全局目录等).要查找为 ipython 会话加载的配置文件,请使用 matplotlib.matplotlib_fname().这是我得到的:

There are various matplotlibrc files that can be changed (i.e. for just a local directory, or globally, etc.). To find the configuration file that's been loaded for your ipython session, use matplotlib.matplotlib_fname(). Here's what I got:

In [1]: import matplotlib

In [2]: matplotlib.matplotlib_fname()
Out[2]: u'/usr/local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'

打开rc文件并更改行:

Opening the rc file and changing the line:

backend      : macosx

阅读:

backend      : Qt4Agg

帮我解决了这个问题.

这篇关于无法手动关闭Matplotlib图窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆