python制作加减速曲线离散点图以及matplotlib.pyplot图表标签文字显示不全的解决方法

一、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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值