【发布时间】:2014-10-23 16:46:28
【问题描述】:
我实际上是在编写一个程序,该程序将样条曲线拟合到我在 matplotlib 窗口上单击的点。我在 matplotlib 网站上使用LineBuilder 类作为示例(下面的代码,cmets 解释了我插入的代码)。但是,当我单击绘图窗口的某个区域时,我想退出绘图。我的代码可以在一台计算机上运行(我相信 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.4.1 或 1.4.2。此行为已恢复。
-
@matsjoyce - 没有错误消息。
-
@tcaswell - 是的,它在 1.4.1 中消失了。感谢您的建议!
标签: python matplotlib tkinter blocking