matplotlib pick_event似乎返回错误的索引

mfuanj7w  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(177)

我有一个使用Matplotlib和Python的3D散点图。我的目标是突出显示一个点,如果它被点击。理论上,文档显示的正是我想要的:Simple Picking Example。此外,我已经看到了关于此功能的几个StackOverflow问题。然而,on_pick函数似乎返回了一个错误的索引。
我使用文档的Simple Picking Example的稍微改编版本:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. points_3d = np.random.rand(10, 3)
  4. fig = plt.figure()
  5. ax = fig.add_subplot(projection='3d')
  6. scatter = ax.scatter(points_3d[:, 0], points_3d[:, 1], points_3d[:, 2], picker=True, c='blue')
  7. def onpick(event):
  8. if event.artist != scatter:
  9. return
  10. n = len(event.ind)
  11. if not n:
  12. return
  13. for dataind in event.ind:
  14. pt = points_3d[dataind]
  15. ax.scatter(pt[0], pt[1], pt[2], c='red', marker='D', s=100)
  16. fig.canvas.draw()
  17. return True
  18. fig.canvas.mpl_connect('pick_event', onpick)
  19. plt.show()

字符串
结果行为如下:

为什么pick_event的索引不对应于正确的3D点?

dsf9zpds

dsf9zpds1#

好吧,这似乎是一个已知的bug。我使用的是matplotlib v3.5.0,升级到v3.8.1修复了它。

相关问题