【问题标题】:Matplotlib - How to plot lines of minimum and maximum gradient through error bars?Matplotlib - 如何通过误差线绘制最小和最大梯度线?
【发布时间】: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


    【解决方案1】:

    我不清楚其他程序显示的曲线是什么。看起来它可能只是通过点画一条线

      (x[0], y[0]-yerror[0]), (x[-1], y[-1]+yerror[-1])
      (x[0], y[0]+yerror[0]), (x[-1], y[-1]-yerror[-1])
    

    使用 matplotlib 绘图应该很容易。但这对我来说似乎没有什么特别的意义。

    另一方面,如果您想在绘图中包含一些拟合结果不确定性的度量,那么有几个选项。首先,您可以使用取自 best_fit 值的参数值和这些值的不确定性来评估模型。例如,你可能会这样做(一旦你有result):

    pars = result.params
    
    curve1 = bestfit(x, pars['m'].value+pars['m'].stderr, pars['c'].value)
    curve2 = bestfit(x, pars['m'].value-pars['m'].stderr, pars['c'].value)
    curve3 = bestfit(x, pars['m'].value, pars['c'].value+pars['c'].stderr)
    curve4 = bestfit(x, pars['m'].value, pars['c'].value-pars['c'].stderr)
    curve5 = bestfit(x, pars['m'].value+pars['m'].stderr, pars['c'].value+pars['c'].stderr)
    curve6 = bestfit(x, pars['m'].value+pars['m'].stderr, pars['c'].value-pars['c'].stderr)
    curve7 = bestfit(x, pars['m'].value-pars['m'].stderr, pars['c'].value+pars['c'].stderr)
    curve8 = bestfit(x, pars['m'].value-pars['m'].stderr, pars['c'].value-pars['c'].stderr)
    

    然后绘制其中的一些。

    使用ModelResult 的eval_uncertainties 方法可以制作更简单且信息量更大的图。看 https://lmfit.github.io/lmfit-py/model.html#calculating-uncertainties-in-the-model-function 一个简单的用法是:

    dely = result.eval_uncertainty()
    plt.fill_between(x, result.best_fit-dely, result.best_fit+dely, color="#ABABAB")
    

    此外,由于您对y 有不确定性,因此您可以自己使用它们。要使用lmfit.Model 执行此操作,您需要将1.0/yerror 作为weight 传递给fit 方法:

    result = mod.fit(y, params, x=x, weights=1.0/np.array(yerror))
    

    (注意:数据和不确定性实际上应该是 numpy 数组,而不是列表)。我认为对于您的数据(不确定性大小相似),它不会对最佳拟合值和不确定性产生太大影响,但会对报告的统计数据(例如卡方)产生影响。

    最后,对于一个简单的线性模型(即参数为线性的模型,例如这个模型),您实际上不需要使用像 lmfit.Model 这样的迭代方法,但可以使用线性回归方法.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多