您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。