Get The Subplot From Pick Event With Matplotlib And Python
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
Solution 1:
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.
Post a Comment for "Get The Subplot From Pick Event With Matplotlib And Python"