1 绘制二维曲线图
import matplotlib.pyplot as plt # 为方便简介为plt
import numpy as np # 画图过程中会使用numpy
import pandas as pd # 画图过程中会使用pandas
def PythonPlot(x: list, y: list):
#绘制曲线图
plt.rcParams['font.family'] = ['SimHei'] #确保图中中文字体正确显示
plt.rcParams['axes.unicode_minus'] = False #确保负号正确显示
fig = plt.figure() #先创建一个窗口
plt.plot(x, y, color = 'blue', marker = 'o') #plot()画出曲线,并设置曲线颜色,线标记
plt.legend(labels = ['二次曲线图'], loc = 'best') #best表示自动分配最佳位置
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('曲线图')
plt.grid()
plt.show() #显示图像
x = np.arange(-1,5,0.01)
y = [a * a - 4 * a + 3 for a in x]
PythonPlot(x, y)
结果为,
2 绘制三维曲线图
import os
import sys
import time
import matplotlib.pyplot as plt
if __name__ == "__main__":
start_t = time.time()
input_txt_path = "KeyFrameTrajectory_kitti_00.txt"
x_coordinates = []
y_coordinates = []
z_coordinates = []
with open(input_txt_path, "r", encoding = "utf-8") as fin:
lines = fin.readlines()
for line in lines:
line = line.replace("\n", "")
ele = line.split()
x = float(ele[0])
y = float(ele[1])
z = float(ele[2])
x_coordinates.append(x)
y_coordinates.append(y)
z_coordinates.append(z)
#plot trajectory
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x_coordinates, y_coordinates, z_coordinates, label='3D Curve')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
plt.savefig("traj.png")
plt.show()
end_t = time.time()
print(f"used time: {(end_t - start_t) / 60.0} minutes!")