【发布时间】:2017-03-21 23:29:44
【问题描述】:
更有趣:我正在尝试使用矩形补丁(具有固定宽度)注册 on_release 事件后传递值。
一切都按预期工作,除了我无法将值传递给所需的测试函数print_me(除非我在做一些完全愚蠢的事情)。
另外,我试图在on_release 发生后通过文本在 y 顶点设置值,但同样没有运气。
理想情况下,我希望有 2 条可拖动的水平线,但我认为这可行。
我的测试代码是:
# from http://stackoverflow.com/questions/12052379/matplotlib-draw-a-selection-area-in-the-shape-of-a-rectangle-with-the-mouse
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
class Annotate(object):
def __init__(self):
self.ax = plt.gca()
self.rect = Rectangle((0,0), 1000, 1, alpha=.5, ec="gray", fc="w", zorder=1)
print(self.rect)
self.x0 = None
self.y0 = None
self.x1 = None
self.y1 = None
self.ax.add_patch(self.rect)
self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
def on_press(self, event):
print ('press')
self.x0 = event.xdata
self.y0 = event.ydata
def on_release(self, event):
print ('release')
#self.x1 = event.xdata
self.y1 = event.ydata
print(self.y0)
print(self.y1)
self.rect.set_width(50000)
self.rect.set_height(self.y1 - self.y0)
self.rect.set_xy((-10, self.y0))
self.text.set_text(str(self.y0))
self.text.set_position((self.get_path()))
self.ax.figure.canvas.draw()
print_me(str(self.y1))
a = Annotate()
plt.show()
def print_me(v):
print('Yo!')
print(v)
【问题讨论】:
标签: python matplotlib interactive