【问题标题】:How can I make a simple 3D line with Matplotlib?如何使用 Matplotlib 制作简单的 3D 线?
【发布时间】:2012-07-17 11:22:15
【问题描述】:

我想生成从 3D 数组中获得的线条。

代码如下:

VecStart_x = [0,1,3,5]
VecStart_y = [2,2,5,5]
VecStart_z = [0,1,1,5]
VecEnd_x = [1,2,-1,6]
VecEnd_y = [3,1,-2,7]
VecEnd_z  =[1,0,4,9]

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot([VecStart_x ,VecEnd_x],[VecStart_y,VecEnd_y],[VecStart_z,VecEnd_z])
plt.show()
Axes3D.plot()

我得到了那个错误:

ValueError: 第三个参数必须是格式字符串

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    抱歉,最后一个链接中的示例不是一条线,而是一个密集的点集合。如果您想绘制一条简单的线性线,则需要起点和终点,并在传统的 2D 图中使用与“-”等效的东西:我猜这个问题与这种线有关:我也做了找不到答案。

    【讨论】:

    【解决方案2】:

    图库是查找示例的绝佳起点:

    http://matplotlib.org/gallery.html

    这里有一个 3d 线图的例子:

    http://matplotlib.org/examples/mplot3d/lines3d_demo.html

    您看到您需要将 3 个向量传递给 ax.plot 函数。 您实际上是在传递列表列表。 我不知道您所说的开始和结束子列表是什么意思,但以下行应该可以工作:

    ax.plot(VecStart_x + VecEnd_x, VecStart_y + VecEnd_y, VecStart_z +VecEnd_z)
    

    这里我对子列表(串联)求和,以便按轴只有一个列表。

    【讨论】:

      【解决方案3】:

      我猜,你想绘制 4 条线。那你可以试试

      for i in range(4):
          ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i],VecEnd_y[i]],zs=[VecStart_z[i],VecEnd_z[i]])
      

      正如@Nicolas 所建议的,请查看 matplotlib 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-14
        • 2016-11-02
        • 1970-01-01
        • 2014-12-13
        • 1970-01-01
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多