【问题标题】:Determine whether a point lies in a Matplotlib subplot确定一个点是否位于 Matplotlib 子图中
【发布时间】:2016-03-22 14:18:32
【问题描述】:

我有一个带有多个子图的 Matplotlib 无花果。我希望在单击位置 1 的子图时执行一个函数。从事件处理程序生成的事件对象中,我如何确定子图是否被点击?

【问题讨论】:

  • 你能给我们看一些示例代码吗?

标签: python matplotlib subplot


【解决方案1】:

作为属性inaxes 进入回调的event 对象,它是被点击的轴。

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()

【讨论】:

  • 谢谢!然后我将如何计算该子图中的点击坐标?例如,如果 subplot 1 包含一个图像,并且我单击该图像的坐标 0,0,当我访问 event.x、event.y 时我没有得到 0,0
  • xy 在像素空间中 xdataydata 在数据空间中。
  • 您将如何从不同的方法访问(并设置数据)子图中的数据?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 2012-03-13
  • 2018-09-29
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多