【问题标题】:Plot a 3D point using matplotlib and arrays with numpy in Python在 Python 中使用 matplotlib 和带有 numpy 的数组绘制 3D 点
【发布时间】:2016-02-15 04:50:00
【问题描述】:

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

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

TypeError: 'numpy.int32' 类型的对象没有 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()

【问题讨论】:

  • 它失败了,因为.plot() 需要一个点序列,而Point[0] 不是一个序列。
  • 请提供它引发的确切错误。
  • 你能建议我一种方法来使用我在 Point 中输入的数据以使其正常工作吗?对不起,如果这可能太容易了,但我真的是 Python 新手

标签: python arrays numpy matplotlib 3d


【解决方案1】:

正如@cel 在 cmets 图中所说的那样,需要点序列。试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2017-03-14
    • 2019-08-09
    • 2020-06-12
    • 2021-05-05
    • 2022-01-02
    • 2023-02-05
    相关资源
    最近更新 更多