一、python制作加减速曲线离散点图
import numpy as np
import matplotlib.pyplot as plt
def move(start, end, max_speed, acceleration, deceleration, discrete_points_num):
# (起点,终点,最大速度,加速度,减速度,均匀离散点数)
points = []
distance = end - start
acceleration_time = max_speed / acceleration
deceleration_time = max_speed / deceleration
constant_speed_time = (distance - (0.5*acceleration*acceleration_time**2 + 0.5*deceleration*deceleration_time**2)) / max_speed
time_interval=(acceleration_time+constant_speed_time+deceleration_time)/discrete_points_num
t = 0
while True:
if 0<= t <= acceleration_time:
displacement = 0.5 * acceleration * t**2
velocity = acceleration * t
elif acceleration_time < t <= acceleration_time + constant_speed_time:
displacement = (0.5 * acceleration * acceleration_time**2) + (max_speed * (t - acceleration_time))
velocity = max_speed
else:
t_decel = t - acceleration_time - constant_speed_time
displacement = (0.5 * acceleration * acceleration_time**2) + (constant_speed_time * max_speed) + (max_speed * t_decel) - (0.5 * deceleration * t_decel**2)
velocity = max_speed - deceleration * t_decel
position = start + displacement
points.append((t,position))
if position >= end:
break
t += time_interval
return points
points=move(-20, 20, 4, 2, 2, 51)
# 提取时间和距离列表
times = [point[0] for point in points]
distances = [point[1] for point in points]
# 绘制离散点图
plt.plot(times, distances, 'bo')
plt.xlabel("时间")
plt.ylabel("转角")
plt.title("加减速离散曲线")
# matplotlib.pyplot图表中的标签文字不显示时,添加下面这句
plt.rcParams['font.sans-serif'] = ['SimHei']
# 小于0的数字或无法显示负号,需添加下面这句
plt.rcParams['axes.unicode_minus'] = False
#plt.legend()
plt.show()
二、matplotlib.pyplot图表标签文字显示不全问题解决
1、当matplotlib.pyplot图表中标签文字根本不显示时
代码中也显示“ UserWarning: Glyph 36716 (\N{CJK UNIFIED IDEOGRAPH-8F6C}) missing from current font.”这句报错
遇到这种情况,请添加下面这句:
plt.rcParams['font.sans-serif'] = ['SimHei']
2、标签中小于0的数无法显示负号
代码中也提示“UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.”这句报错时,则需添加下面这句即可
plt.rcParams['axes.unicode_minus'] =False