Reputation: 55
I have a figure with four subplots, two of which are binded on a pick event, by doing canvas.mpl_connect('pick_event', onpick)
where onpick is onpick(event) handler.
Now, based on which of the two suplot the click come in, I must activate a different behaviour (ie if pick come from 1st subplot do this, else if it come from second suplot do that), but I don't know how to do it. Can anyone help me?
Upvotes: 1
Views: 1842
Reputation: 4430
Here is a short example:
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()
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.
For further reading, here's the PickEvent
documentation and an pick handling demo from the matplotlib site.
Upvotes: 3