python画三维轨迹图
时间: 2023-07-06 07:41:26 浏览: 298
要画三维轨迹图,可以使用matplotlib库中的mplot3d子库。首先需要导入以下库:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
```
接下来,定义三维数据:
```python
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 6, 2, 3, 13, 4, 1, 2, 4, 8]
z = [1, 2, 3, 3, 5, 7, 9, 11, 9, 10]
```
然后,创建3D坐标轴对象,并设置轴的标签:
```python
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
```
接下来,使用scatter函数画出散点图:
```python
ax.scatter(x, y, z, c='r', marker='o')
```
最后,调用show函数显示图像:
```python
plt.show()
```
完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 6, 2, 3, 13, 4, 1, 2, 4, 8]
z = [1, 2, 3, 3, 5, 7, 9, 11, 9, 10]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
```
阅读全文