matplotlib 1.3中的plt.show()块

时间:2014-10-23 16:46:29

标签: python matplotlib tkinter blocking

我实际上是在编写一个程序,使样条符合我在matplotlib窗口上单击的点。我在matplotlib网站上使用LineBuilder类作为示例(下面的代码,注释解释了我插入的代码)。但是,当我点击绘图窗口的某个区域时,我想退出绘图。我在一台计算机上运行的代码(matplotlib 1.2我相信)。在另一台计算机(matplotlib 1.3)上,单击窗口的相应部分后,它不会继续plt.show()之后的代码。相反,当我退出GUI时,它决定运行plt.show()之后的代码。

有人知道可能导致这种情况的原因吗?我不确定这个问题的确切性质。我知道如果我在block=False中转plt.show(),代码就会运行,但我无法构建我的行,所以我觉得它可能与此有关。但我无法找到是否已经改变。代码:

from matplotlib import pyplot as plt
class LineBuilder:
    def __init__(self, line): 
        self.line = line
        self.xs = list(line.get_xdata())
        self.ys = list(line.get_ydata()) 
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self) 
    def __call__(self, event):
        print 'click', event
        if event.inaxes!=self.line.axes: return 
        self.xs.append(event.xdata)
        self.ys.append(event.ydata) 
        self.line.set_data(self.xs, self.ys) 
        self.line.figure.canvas.draw()
        #If x.data < previous, plt.close ('all')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])
linebuilder = LineBuilder(line)
plt.show ()
#Code that follows does not run in newer (?) version

1 个答案:

答案 0 :(得分:0)

Matplotlib有一个交互式和非交互式模式,我发现这取决于系统配置甚至你如何启动你的scipt(系统shell,交互式shell,专用shell,IDLE shell ......)这个可能会有所不同。

如果您在代码的开头某处调用matplotlib.interactive(True),则应避免这种情况。它在Matplotlib 0.99和1.3.1中有效,分别在python 2.65和2.75上。 plt.ion()也应该切换模式,虽然我还没有测试过。