最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python matplotlib拟合直线如何实现 python matplotlib拟合直线实现代码
时间:2019-11-19 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python matplotlib拟合直线实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
代码如下
import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] def linear_regression(x, y): N = len(x) sumx = sum(x) sumy = sum(y) sumx2 = sum(x ** 2) sumxy = sum(x * y) A = np.mat([[N, sumx], [sumx, sumx2]]) b = np.array([sumy, sumxy]) return np.linalg.solve(A, b) #单臂 #修改数据1: X1=np.array([0,20,40,60,80,100,120,140,160,180,200]) Y1=np.array([0,0.02,0.06,0.1,0.13,0.16,0.19,0.22,0.245,0.278,0.3]) #半桥 #修改数据2: X2=np.array([0,20,40,60,80,100,120,140,160,180,200]) Y2=np.array([0,0.057,0.118,0.185,0.245,0.308,0.376,0.425,0.488,0.544,0.58]) a0, a1 = linear_regression(X1, Y1) # 生成拟合直线的绘制点 _X1 = [0, 200] _Y1 = [a0 + a1 * x for x in _X1] a0, a1 = linear_regression(X2, Y2) # 生成拟合直线的绘制点 _X2 = [0, 200] _Y2 = [a0 + a1 * x for x in _X1] #显示图像 plt.plot( X1, Y1, 'ro', line,label="单臂电桥") plt.plot(_X1, _Y1, 'b',line,label='单臂电桥',color='C0') plt.plot( X2, Y2, 'g^', line,label='半桥') plt.plot(_X2, _Y2, 'b', line,label='半桥',color='C1') plt.xlabel('weight/g') plt.ylabel('voltage/v') plt.legend() plt.show()
图例
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27