matplotlib pick_event似乎返回错误的索引

mfuanj7w  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(129)

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

import matplotlib.pyplot as plt
import numpy as np

points_3d = np.random.rand(10, 3)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')

scatter = ax.scatter(points_3d[:, 0], points_3d[:, 1], points_3d[:, 2], picker=True, c='blue')

def onpick(event):
    if event.artist != scatter:
        return
    n = len(event.ind)
    if not n:
        return
    for dataind in event.ind:
        pt = points_3d[dataind]
        ax.scatter(pt[0], pt[1], pt[2], c='red', marker='D', s=100)
    fig.canvas.draw()
    return True

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

字符串
结果行为如下:

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

dsf9zpds

dsf9zpds1#

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

相关问题