在 Python 中使用 matplotlib 和带有 numpy 的数组绘制 3D 点

Plot a 3D point using matplotlib and arrays with numpy in Python

我想使用 matplotlib 用数组绘制一个点,但它无法读取数组中的数据。

这里提供了一个测试用例,如果我使用plt.plot([1], [1], [1], 'or')它可以工作,但是如果我使用plt.plot(Point[0], Point[1], Point[2], 'or')它会失败生成错误:

TypeError: object of type 'numpy.int32' has no len()

有什么建议吗?

感谢您的帮助!

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

ax.set_xlim3d(0,3)
ax.set_ylim3d(0,3)
ax.set_zlim3d(0,3)

Point = np.array([1,1,1])
plt.plot([1], [1], [1], 'or')
plt.plot(Point[0], Point[1], Point[2], 'or')

plt.show()

正如 @cel 在评论中所说的那样,情节需要点的顺序。例如试试这个:

plt.plot([[1]], [[1]], [[1]], 'or')
plt.plot([Point[0]], [Point[1]], [Point[2]], 'or')