交互模式不从脚本开启(mac osx 10.8.5上的matplotlib 1.4.0 python 2.7.5)

时间:2014-09-21 14:03:56

标签: macos python-2.7 matplotlib

在我的mac上使用python 2.6,以下工作正常(即打开一个绘图窗口):

import matplotlib.pyplot as plt
from numpy import linspace, sin, pi
plt.ion()
print "Is interactive:?", plt.isinteractive()

x = linspace(-pi, pi, 1001)
plt.plot(x, sin(x))

raw_input() #keep the window open

当我在shell(即$ python test.py)中运行它以及在交互式python终端中运行它时,它可以工作。

我最近安装了python 2.7,当我从shell运行我的脚本时,没有任何事情发生(更准确地说,绘图窗口出现在Dock中,但是没有打开)。即使在plt.ion()之后,plt.isinteractive()的值也是假的。

当我在交互式python终端中运行相同的代码时,一切都很好。

this question的答案使得绘图窗口出现,但我觉得很烦人,现在我必须将plt.pause(0.1)添加到我的脚本中。

有没有办法在不修改代码的情况下获得早期行为?

后端是macosx。

1 个答案:

答案 0 :(得分:1)

这似乎是与matplotlib 1.4相关的bug。一个丑陋的解决方法是包括:

import sys
sys.ps1 = 'SOMETHING'

在导入matplotlib之前。

或者,可以使用ipython来运行脚本。

有关详细信息,请参阅此处https://github.com/matplotlib/matplotlib/issues/3505