【发布时间】:2018-10-28 11:05:35
【问题描述】:
我的代码:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
def bestfit(x, m, c):
return m * x + c
x = [2.8672E-02, 2.2199E-02, 1.8180E-02, 1.5410E-02, 1.3325E-02]
y = [8.64622E-03, 7.07473E-03, 6.13109E-03, 5.46607E-03, 4.90341E-03]
xerror =[8.2209E-07, 4.9280E-07, 3.3052E-07, 2.3748E-07, 1.7756E-07]
yerror = [1.62083E-04, 1.45726E-04, 1.38127E-04, 1.26587E-04, 1.22042E-04]
mod = Model(bestfit)
params = mod.make_params(m = 0.2421, c = 0.0017)
result = mod.fit(y, params, x = x)
print(result.fit_report())
print(1 - result.residual.var() / np.var(y))
matplotlib.rcParams['font.serif'] = "Times New Roman"
matplotlib.rcParams['font.family'] = "serif"
plt.plot(x, y, 'bo', markersize = 1.5)
plt.plot(x, result.best_fit, color = 'red', linewidth = 0.5)
plt.xlabel(r'Inverse Mass $g^{-1}$')
plt.ylabel('Damping Coefficient $s^{-1}$')
plt.errorbar(x, y, xerror, yerror)
plt.show()
我希望创建最小和最大梯度线,以及它们的方程式,如下所示:
我可以在 Excel 中完成,但这需要手动输入 4 个极端数据点。
如何自动执行此操作?
【问题讨论】:
标签: python matplotlib plot graph