您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了怎么在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', linewidth=2,label="单臂电桥") plt.plot(_X1, _Y1, 'b',linewidth=2,label='单臂电桥',color='C0') plt.plot( X2, Y2, 'g^', linewidth=2,label='半桥') plt.plot(_X2, _Y2, 'b', linewidth=2,label='半桥',color='C1') plt.xlabel('weight/g') plt.ylabel('voltage/v') plt.legend() plt.show()
图例
上述内容就是怎么在python中利用matplotlib拟合直线,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。