简体   繁体   中英

Determine whether a point lies in a Matplotlib subplot

I've got a Matplotlib fig with several subplots. I want a function to be executed when the subplot in position 1 is clicked. From the event object that is generated by the event handler how do I determine whether or not the subplot was clicked?

The event object that comes into the callback as an attribute inaxes which is the axes which was clicked.

import matplotlib.pyplot as plt
fig, ax_list = plt.subplots(2, 2)
ax_list = ax_list.ravel()
ax_list[0].set_gid('A')
ax_list[1].set_gid('B')
ax_list[2].set_gid('C')

def clicker(event):
    print(event.inaxes.get_gid())

fig.canvas.mpl_connect('button_press_event', clicker)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM