Python【Matplotlib】鼠标单击事件判断点击的是否为图例
发布时间:2023年12月19日
直接上代码:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], label='Line 1')
ax.legend(draggable=True)
legend = ax.get_legend()
legend_bbox = legend.get_window_extent()
def call_move(event):
axtemp = event.inaxes
mouse_x = event.x
mouse_y = event.y
print(f"鼠标_x={mouse_x}")
print(f"鼠标_y={mouse_y}")
if axtemp and axtemp.get_legend():
legend_bbox = axtemp.get_legend().get_window_extent()
left_bottom = legend_bbox.get_points()[0]
right_top = legend_bbox.get_points()[1]
if left_bottom[0] <= mouse_x <= right_top[0] and left_bottom[1] <= mouse_y <= right_top[1]:
print("鼠标点击在图例矩形区域内。")
else:
print("鼠标点击在图例矩形区域外。")
fig.canvas.mpl_connect('button_press_event', call_move)
plt.show()
文章来源:https://blog.csdn.net/qq_43324506/article/details/135017226
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!