import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fpath = r”C:\Python\ripple\quest1532.csv”
name = fpath.split(“\\”)[-1].split(“.”)[0] #quest1532
dfRaw = pd.read_csv(fpath, header= None)
#[82 rows x 2 columns]
lisCol = dfRaw.iloc[0].tolist()
#[‘Theta (deg) – 15000 MHz’, ‘Amplitude (dBi) – 15000 MHz’]
dfRaw_drop = dfRaw.drop([0], axis=0)
aryTheta = dfRaw_drop[0].values.astype(np.float64)
#(81,)
aryAmp = dfRaw_drop[1].values.astype(np.float64)
#(81,)
coeff = np.polyfit(aryTheta, aryAmp, 2)
#array([-1.25610745e-02, -7.17384029e-02, 2.01926723e+01])
aryAmpFit = coeff[0]*aryTheta**2 + \
coeff[1]*aryTheta**1 +\
coeff[2]*aryTheta**0 #(81,)
p = np.poly1d(coeff)
aryAmpFit2 = p(aryTheta)
#與aryAmpFit一樣
fig, ax = plt.subplots()
ax.plot(aryTheta,aryAmp,”b-“, label=lisCol[1] )
ax.plot(aryTheta,aryAmpFit,”ro”, label=lisCol[1] + “_Fit”)
#要使用legend() , plot() 要指定label參數
ax.legend(loc = “best”)
ax.set_xlabel(“Theta”)
ax.set_ylabel(“Amplitude(dB)”)
ax.set_title(name+” Fit vs Real”, fontsize=18)
fig.savefig(name + “.png”)
aryDif = (aryAmpFit – aryAmp) #(81,)
err = np.sum( aryDif**2 ) / len(aryDif)
print(“%s error: %s” %(name,err) )
# =======================================
#err = np.sum( aryDif**2 ) / len(aryDif) 結果一樣
# err = 0
# for dif in aryDif:
# err += dif**2 / len(aryDif)
## /len(aryDif) 應該在for迴圈之外
#err = err/len(aryDif)
# =======================================
aryAmpFit與aryAmpFit 2
結果一樣:
輸出的圖檔:
推薦hahow線上學習python: https://igrape.net/30afN
rcParams是处理 Matplotlib 属性和默认样式的字典。
ax.legend(loc = “best”)
“””ax.legend(loc = “best”, fontsize=16,
handlelength=2, frameon = False)
#frameon = False 不要外框
#ax.legend()的fontsize參數也可指定
#handlelength可調整 #图例句柄长度”””
下方多一行
plt.rc(“legend”, fontsize=16)
plt.rcParams
有很多參數可以調整
更新此字典也可改變legend
legend相關的部分:
‘legend.fontsize’: 16.0,
‘legend.handlelength’: 2.0,
#图例句柄长度
ax.legend(loc = “best”, fontsize=16,
handlelength=2, frameon = False)
ax.legend(loc = “best”)
plt.rc(“legend”, fontsize=16)
下方再加一行:
plt.rcParams.update({‘legend.handlelength’: 0.5})
或 plt.rcParams[“legend.handlelength”]=0.5
同dict的更新方式
Amplitude左方的藍線變短了
推薦hahow線上學習python: https://igrape.net/30afN
近期留言