Python Matplotlib 实现时间和三维位置曲线图
这里提供一种使用 matplotlib 库实现时间和三维位置的曲线图的 Python 代码:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 时间序列
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 三维位置序列
x = [1, 2, 4, 5, 6, 8, 9, 10, 11, 12]
y = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
z = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# 创建3D坐标系
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制三维曲线
ax.plot(x, y, z, label='position')
# 设置坐标轴标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# 绘制时间曲线
ax.plot(x, y, time, label='time')
# 添加图例
plt.legend()
# 显示图形
plt.show()
这段代码首先定义了时间序列和三维位置序列,然后创建了一个 3D 坐标系,并使用 plot() 函数绘制了三维曲线和时间曲线。最后,通过 set_xlabel()、set_ylabel() 和 set_zlabel() 函数设置了坐标轴标签,并通过 legend() 函数添加了图例。最终使用 show() 函数显示图形。
原文地址: https://www.cveoy.top/t/topic/nlcS 著作权归作者所有。请勿转载和采集!