希望有人能在我的第一个程序中告诉我我做错了什么 回调。
目标:
self.currentLine
)在用户输入结束时,应该有四条垂直线和类 应该返回一个包含x坐标的列表。
目前,我无法找出更新线对象的正确方法 情节(即我可以获得我想要的鼠标跟踪效果)。我也拿不到 完成后返回值列表的类。
我知道while
循环可能不是正确的方法,但我找不到合适的方法。
import matplotlib.pyplot as plt
import pdb
class getBval:
def __init__(self):
figWH = (8,5) # in
self.fig = plt.figure(figsize=figWH)
plt.plot(range(10),range(10),'k--')
self.ax = self.fig.get_axes()[0]
self.x = [] # will contain 4 "x" values
self.lines = [] # will contain 2D line objects for each of 4 lines
self.connect = self.ax.figure.canvas.mpl_connect
self.disconnect = self.ax.figure.canvas.mpl_disconnect
self.mouseMoveCid = self.connect("motion_notify_event",self.updateCurrentLine)
self.clickCid = self.connect("button_press_event",self.onClick)
def updateCurrentLine(self,event):
xx = [event.xdata]*2
self.currentLine, = self.ax.plot(xx,self.ax.get_ylim(),'k')
plt.show()
def onClick(self, event):
if event.inaxes:
self.updateCurrentLine(event)
self.x.append(event.xdata)
self.lines.append(self.currentLine)
del self.currentLine
if len(self.x)==4:
self.cleanup()
def cleanup(self):
self.disconnect(self.mouseMoveCid)
self.disconnect(self.clickCid)
return self
xvals = getBval()
print xvals.x
答案 0 :(得分:0)
我建议您为垂直线使用Cursor
窗口小部件,只收集点击次数的x值(而不是整个Line2D
),这是一个示例:
import matplotlib.pyplot as plt
import matplotlib.widgets as mwidgets
class getBval:
def __init__(self):
figWH = (8,5) # in
self.fig = plt.figure(figsize=figWH)
plt.plot(range(10),range(10),'k--')
self.ax = self.fig.get_axes()[0]
self.x = [] # will contain 4 "x" values
self.lines = [] # will contain 2D line objects for each of 4 lines
self.cursor = mwidgets.Cursor(self.ax, useblit=True, color='k')
self.cursor.horizOn = False
self.connect = self.ax.figure.canvas.mpl_connect
self.disconnect = self.ax.figure.canvas.mpl_disconnect
self.clickCid = self.connect("button_press_event",self.onClick)
def onClick(self, event):
if event.inaxes:
self.x.append(event.xdata)
if len(self.x)==4:
self.cleanup()
def cleanup(self):
self.disconnect(self.clickCid)
plt.close()
xvals = getBval()
plt.show()
print xvals.x