在 python 中向散点图添加一个点

Add a point to a scatter plot in python

我有一个点云,为了展示它,我使用了 matpyplot 库。然后我想在这个图中添加一个点,它是那朵云的质心。这是我的代码

point_list, centroid_list = points(dataset[:, :7])
# take one point cloud 
points = np.array(point_list[0])
# take the centroid of that point cloud
centroid = np.array(centroid_list[0])
f = plt.figure(figsize=(15, 8))
ax = f.add_subplot(111, projection='3d')
ax.scatter(*np.transpose(points[:, [0, 1, 2]]), s=1, c='#FF0000', cmap='gray')
ax.plot(centroid, 'or') # this line doesn't work
plt.show()

画点云的线是我的问题,不知道怎么加点。我在其他线程中尝试过一些解决方案,但它们不起作用。我想用另一种颜色绘制质心,也许更大,例如使用散点图的符号 s=5, c='#00FF00'

以下工作代码应该能让您有所了解。在您的代码中,质心的 dimensions/shape 不知何故不连贯。您可能需要相应地调整该行。

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

points = np.random.normal(size=(20,3))
# take the centroid of that point cloud
centroid = np.asarray([0.,0.,0.])
f = plt.figure(figsize=(15, 8))
ax = f.add_subplot(111, projection='3d')
ax.scatter(*np.transpose(points[:, [0, 1, 2]]), s=1, c='#FF0000', cmap='gray')
ax.plot([centroid[0]],[centroid[1]],[centroid[2]],'or') # this line doesn't work
plt.show()