Python 如何使用Matplotlib制作简单的三维线?
我想生成线,这是我从三维数组中得到的 代码如下:Python 如何使用Matplotlib制作简单的三维线?,python,matplotlib,Python,Matplotlib,我想生成线,这是我从三维数组中得到的 代码如下: 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()
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:第三个参数必须是格式字符串
图库是查找示例的绝佳起点: 这里有一个3d线图示例: 您可以看到,需要传递到ax.plot函数3向量。 实际上,您正在传递列表列表列表。 我不知道“开始”和“结束”子列表是什么意思,但以下行应该可以:
ax.plot(VecStart_x + VecEnd_x, VecStart_y + VecEnd_y, VecStart_z +VecEnd_z)
在这里,我对子列表求和(连接),以便按轴只有一个列表。我想,您需要绘制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画廊