plot无法画图---已解决

项目场景:plot画图—BUG两则

记录小bug:

问题描述

用vscode开发中,需要进行plot输出,之前一直没问题,近日plot无输出,程序直接结束,不报错。
百度方案种种,无果。
测试样例:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0, 69, 1)
plt.plot(t, t, 'r', t, t**2, 'b')
label = ['t', 't**2']
plt.legend(label, loc='upper left')
plt.savefig('./test2.jpg')
plt.show()

原因分析:

提示:matplotlib包问题,重装,问题依旧


解决方案:

无奈放到pycharm下测试,发现程序

进程已结束,退出代码-1066598274 (0xC06D007E)

百度该退出代码,发现问题:
【-1066598274 (0xC06D007E)】解决matplotlib版本冲突问题
https://blog.youkuaiyun.com/weixin_40908748/article/details/127015859

  1. 卸载matplotlib和numpy库
pip uninstall matplotlib
pip uninstall numpy
  1. 查看anaconda环境中这两个库是否删干净了
pip list
  1. 重新安装matplotlib
pip install matplotlib

注意,只安装matplotlib即可,numpy作为依赖包会自动进行安装,plot显示正常,bug1解决。

问题描述:

运行另一个程序,依旧显示:

退出代码-1066598274 (0xC06D007E)

程序示例

import time
import matplotlib.pyplot as plt
import numpy as np
from math import e
from scipy.interpolate import make_interp_spline as spline

class PID:
    def __init__(self, P=0.2, I=0.0, D=0.0):
        self.kp = P
        self.ki = I
        self.kd = D
        self.uPrevious = 0
        self.uCurent = 0
        self.setValue = 0
        self.lastErr = 0
        self.preLastErr = 0
        self.errSum = 0
        self.errSumLimit = 10
# 位置式PID
    def pidPosition(self, curValue):
        err = self.setValue - curValue
        dErr = err - self.lastErr
        self.preLastErr = self.lastErr
        self.lastErr = err
        self.errSum += err
        outPID = self.kp * err + (self.ki * self.errSum) + (self.kd * dErr)
        return outPID

# 增量式PID
    def pidIncrease(self, curValue):
        self.uCurent = self.pidPosition(curValue)
        outPID = self.uCurent - self.uPrevious
        self.uPrevious = self.uCurent
        return outPID


class BeControlled:
    def __init__(self):
        self.lastControlIn = 0
        self.preLastControlIn = 0
        self.lastControlOut = 0
        self.preLastControlOut = 0
# 被控对象的相关计算
    def beControlledDeal(self, outPID):
        # output = 2*self.lastControlOut - 1*self.preLastControlOut + \
        #     0.00005*self.lastControlIn + 0.00005*self.preLastControlIn

        # output为被控对象的输出,此处是被控对象的传递函数离散化后,写成差分方程后的形式,被控对象的方程此处直接采用了设计好的参数,并与PID控制器的输出进行计算。
        # 如果需要设计自己的被控对象,将传递函数进行z变换后,交叉相乘,再进行z反变换即可,可参考《计算机控制系统》等书籍。
        # 因为是单位反馈,所以被控对象的输出等于传递函数的输入。
        output = 0.00019346*self.preLastControlIn + 0.00019671e-04*self.lastControlIn + \
            1.9512*self.lastControlOut - 0.9512*self.preLastControlOut
        self.preLastControlIn = self.lastControlIn
        self.lastControlIn = outPID
        self.preLastControlOut = self.lastControlOut
        self.lastControlOut = output
        return output


def testPid(P=0.2, I=0.0, D=0.0, Len=1000):
    pid = PID(P, I, D)
    beControlled = BeControlled()
    pid.setValue = 1  # set end
    curValue = 0
    curValueList = []
    timeList = []
    setValueList = []
    PIDoutList = []
    curValueList.append(0)
    timeList.append(0)
    setValueList.append(pid.setValue)
    PIDoutList.append(0)
    for i in range(1, Len):
        #采用位置式PID去掉注释即可
        # outPID = pid.pidPosition(curValue)
        outPID = pid.pidIncrease(curValue)
        PIDoutList.append(outPID)
        curValue = beControlled.beControlledDeal(outPID)
        curValueList.append(curValue)
        setValueList.append(pid.setValue)
        timeList.append(i)
    # 绘图
    timeSm = np.array(timeList)
    timeSmooth = np.linspace(timeSm.min(), timeSm.max(), 300)  # 将x轴300等分
    curValueSmooth = spline(timeList, curValueList)(timeSmooth)  # 插值.使原y轴数据平滑 
    pidoutSmooth = spline(timeList, PIDoutList)(timeSmooth)  # 使PID控制器输出平滑
    plt.figure(figsize=(4,3),facecolor='blue')
    #plt.xticks([-1, 3, 5])
    #plt.yticks([-1, 100, 10])
    plt.plot(timeSmooth, curValueSmooth)  # 画被控对象输出
    print(11)
    plt.plot(timeSmooth, pidoutSmooth)  # 画PID控制器输出
    plt.plot(timeList, setValueList)  # 画直线
    plt.xlim((0, Len))
    plt.ylim((min(curValueList)-0.5, max(curValueList)+0.5))
    plt.xlabel('time (s)')
    plt.ylabel('set value')
    plt.title('PID')
    plt.ylim((1-0.5, 1+0.5))
    plt.grid(True)
    plt.show()


if __name__ == "__main__":
    testPid(P=0, I=0.5, D=0, Len=5000)

原因分析

在bug1解决的时候安装matplotlib提示:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
scipy 1.7.1 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.23.3 which is incompatible.

解决方案:

卸载 重装scipy库,会自动根据已安装的numpy版本来安装合适版本,GOOD!bug2解决!

pip uninstall Scipy
pip install  scipy
### 解决 Matplotlib Plot 中文不显示问题 MatplotlibPython 的一个重要绘工具,但在不同操作系统上可能会遇到中文字符无法正常显示的问题。以下是针对 Windows、Mac 和 Linux (Ubuntu) 系统的具体解决方案。 #### 通用设置方法 在导入 `matplotlib` 后,可以通过配置 `rcParams` 来指定字体支持中文。例如: ```python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体字 plt.rcParams['axes.unicode_minus'] = False # 解决负号 '-' 显示为方块的问题 ``` 上述代码通过设置 `font.sans-serif` 参数来加载中文字体[^1]。如果系统未安装对应的字体,则需要先下载并安装该字体。 --- #### 针对 Mac 系统的解决方案 在 macOS 上,默认情况下可能无法自动识别中文字体。可以尝试手动指定一种兼容的中文字体,例如 `'Arial Unicode MS'` 或其他已知可用的字体[^2]。具体实现如下: ```python import matplotlib.pyplot as plt plt.rcParams['font.family'] = 'Arial Unicode MS' plt.rcParams['font.size'] = 12 plt.rcParams['axes.unicode_minus'] = False ``` 如果没有预装合适的字体,可以从网上下载适合的 TTF 字体文件,并将其放置到系统的字体目录中。 --- #### Ubuntu/Linux 下的解决方案 在 Ubuntu 或其他 Linux 发行版中,由于默认缺少中文字体的支持,通常会看到乱码或者空白框的现象。解决此问题的一个有效方式是安装额外的中文字体包[^3]。操作步骤如下: 1. 安装中文字体库: ```bash sudo apt-get install fonts-wqy-microhei ``` 2. 修改 Matplotlib 配置以使用新安装的字体: ```python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei'] plt.rcParams['axes.unicode_minus'] = False ``` 完成以上两步后即可正常使用中文绘制表。 --- #### 测试代码示例 以下是一个完整的测试脚本,用于验证中文是否能够正确显示: ```python import matplotlib.pyplot as plt # 设置字体参数 plt.rcParams['font.sans-serif'] = ['SimHei'] # 替换为你本地支持的字体名称 plt.rcParams['axes.unicode_minus'] = False # 处理负数符号异常 # 创建简单的折线 x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) plt.title('这是一个标题') # 添加带有中文的标题 plt.xlabel('X轴标签') plt.ylabel('Y轴标签') plt.show() ``` 运行这段代码后,应该可以看到包含中文标注的像界面。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值