matplotlib:plt.I交互()返回0,而不是True或False血小板当脚本在终端中运行时调用()?

2024-04-24 00:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的剧本:

from __future__ import division
import numpy as np

import matplotlib
print "matplotlib backend: {}".format(matplotlib.get_backend())

from matplotlib import pyplot as plt
import sys

print "-----------------------"
print "Is matplotlib plotting interactive? {}".format(plt.isinteractive())

if not plt.isinteractive():
    print "matplotlib plotting is not interactive..."
    print "Trying to set matplotlib plotting to be interactive..."
    plt.ion()

print "-----------------------"
print "Is matplotlib plotting interactive now? {}".format(plt.isinteractive())

continue_script = raw_input("Should script continue? [y/[n]]")
if continue_script.strip() != "y":
    sys.exit(0)

plt.draw()

continue_script = raw_input("Should script continue? [y/[n]]")
if continue_script.strip() != "y":
    sys.exit(0)

令人惊讶的是,当在终端(python interactive_plotting.py——最好是,我希望在PyCharm[因此是PyCharm标记]中运行它,但是我也遇到了同样的问题,所以我现在只是在一个普通的gnome-terminal中尝试它),我看到了以下输出:

^{pr2}$

注意Qt4Aggis an interactive backend。现在,如果我继续这个脚本,那么后续的plt.draw()命令不会导致弹出图形。在

如果我只是打开一个Python终端并尝试一下,一切似乎都能正常工作:

Python 2.7.9 |Anaconda 2.1.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> from matplotlib import pyplot as plt
>>> plt.isinteractive()
False
>>> plt.ion()
>>> plt.isinteractive()
True
>>> 

随后,运行plt.draw()命令会弹出一个空图形,我可以在终端中输入其他命令,动态查看更新的图形。如果不在Python解释器中运行脚本,这正是我想要的。在

发生了什么事,我怎样才能让事情运转起来?在


Tags: fromimportbackendformatifmatplotlibassys