项目场景: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.csdn.net/weixin_40908748/article/details/127015859
- 卸载matplotlib和numpy库
pip uninstall matplotlib
pip uninstall numpy
- 查看anaconda环境中这两个库是否删干净了
pip list
- 重新安装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
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
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 = 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
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):
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)
curValueSmooth = spline(timeList, curValueList)(timeSmooth)
pidoutSmooth = spline(timeList, PIDoutList)(timeSmooth)
plt.figure(figsize=(4,3),facecolor='blue')
plt.plot(timeSmooth, curValueSmooth)
print(11)
plt.plot(timeSmooth, pidoutSmooth)
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)