如何从linux上的python脚本使用matplotlib 1.5.1进行交互式绘图?

时间:2016-02-18 00:10:11

标签: python matplotlib

根据过去的经验和http://matplotlib.org/users/shell.html页面,我的理解是我应该使用TkAgg后端。我发现TkAgg没有更新matplotlib 1.5.1或1.5.0中的图,而它是在matplotlib 1.4.3中。我正在运行这个脚本:

import time
import matplotlib as mpl
print mpl.__version__
mpl.rcParams['backend'] = 'TkAgg'

import matplotlib.pyplot as plt
plt.ion()

plt.figure(3)

for xx in range(3):
    plt.ioff()
    plt.plot([1,3,34],[3,4+xx,3])
    plt.draw()
    plt.ion()
    print "sleeping 1 sec"
    time.sleep(1)

并且在1.4.3之下,我每秒都会在绘图中添加一个新行,而在1.5.1中,该图显示,但没有绘制绘图。应该是前者不应该吗?或者我使用ioff / draw / ion有什么问题,还是有其他方法可以使用matplotlib 1.5.1?

我的环境是linux red hat 7,python 2.7.11,我正在使用anaconda conda创建两个不同的环境,即

conda create --name matplotlib-1.5.1 matplotlib=1.5.1
conda create --name matplotlib-1.4.3 matplotlib=1.4.3

所以也许这是anaconda包装的问题?当我在这两个环境中列出软件包时,除了matplotlib-1.4.3依赖于py2cairo 1.10.0,而matplotlib 1.5.1依赖于py2cairo 1.10.0以及一个名为cycler的新东西时,一切都是相同的。 0.9.0。我在http://matplotlib.org/devdocs/users/whats_new.html页面中没有看到任何建议更改的内容。

1 个答案:

答案 0 :(得分:2)

这对我matplotlib 1.4.3没有吸引力。我建议进行一些更改,包括之前添加plt.show()并使用plt.pause(1.)代替time.sleep(),这可能会对新的matplotlib起到更好的作用。以下是否按预期工作,

import matplotlib as mpl
print mpl.__version__
mpl.rcParams['backend'] = 'TkAgg'

import matplotlib.pyplot as plt

plt.figure(3)
plt.ion()
plt.show()

for xx in range(3):
    plt.plot([1,3,34],[3,4+xx,3])
    plt.draw()
    print("sleeping 1 sec")
    plt.pause(1.)

我知道这更像是一个评论,但似乎更容易发布这样的代码(它可能有助于修复)。