更新时间:2023-11-21 15:08:22
这是一个简短的例子:
import matplotlib.pyplot as plt
from random import random
def onpick(event):
if event.artist == plt1:
print("Picked on top plot")
elif event.artist == plt2:
print("Picked on bottom plot")
first = [random()*i for i in range(10)]
second = [random()*i for i in range(10)]
fig = plt.figure(1)
plt1 = plt.subplot(211)
plt.plot(range(10), first)
plt2 = plt.subplot(212)
plt.plot(range(10), second)
plt1.set_picker(True)
plt2.set_picker(True)
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
请注意,您必须在应该触发该事件的子图中调用 set_picker(True)
!如果不这样做,即使将事件设置在画布上也不会发生任何事情.
Note that you have to call set_picker(True)
on the subplots that should fire this event! If you don't, nothing will happen even though you've set the event on the canvas.
如需进一步阅读,请参阅PickEvent
文档 和来自 matplotlib 站点的 pick 处理演示.
For further reading, here's the PickEvent
documentation and an pick handling demo from the matplotlib site.