给定一个有多个图的图,是否有一种方法来确定鼠标按钮点击了其中的哪一个?
例如
fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(imsp0)
ax = fig.add_subplot(122)
ax.imshow(imsp1)
fig.canvas.mpl_connect("button_press_event",onclick_select)
def onclick_select(event):
... do something depending on the clicked subplot
2条答案
按热度按时间c86crjj01#
如果保留两个轴的句柄,则可以只查询发生单击的轴;例如
if event.inaxes == ax:
gwbalxhn2#
这至少可以通过应用以下步骤来实现:
x
和y
,它们携带从图的角开始的像素坐标fig.transFigure.inverted().transform((x,y))
转换成图形坐标bb=ax.get_position()
得到每个子图的边界框bb.contains(fx,fy)
测试点击是否在这个边界框的区域内,其中fx
和fy
是转换成图像位置的按钮点击坐标有关onclick事件的更多信息:http://matplotlib.org/users/event_handling.html有关坐标转换的更多信息:http://matplotlib.org/users/transforms_tutorial.html