更有趣:我试图在使用矩形补丁(固定宽度)注册on_release
事件后传递值。
所有内容都按预期工作,但我无法将值传递给所需的测试函数print_me
(除非我正在做一些完全无聊的事情)。
此外,我试图在on_release
发生后通过y-vertices处的文本设置值,但同样,没有运气。
理想情况下,我希望有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)
答案 0 :(得分:2)
// Animation duration in ms
speed : 100
self.text
,用不同的东西替换它。get_path
函数。完整的工作代码:
print_me