在 scipy 中的曲线拟合线上绘制一个 sigma 误差线 [英] Plotting one sigma error bars on a curve fit line in scipy

查看:73
本文介绍了在 scipy 中的曲线拟合线上绘制一个 sigma 误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 scipy.optimize.curve_fit() 绘制了线性最小二乘拟合曲线.我的数据有一些与之相关的错误,我在绘制拟合曲线时添加了这些错误.

I plotted a linear least square fit curve using scipy.optimize.curve_fit(). My data has some error associated to it and I added those while plotting the fit curve.

接下来,我想在曲线拟合和这两条线之间的阴影区域上绘制两条虚线,代表一个 sigma 误差条.这是我迄今为止尝试过的:

Next, I want to plot two dashed lines representing one sigma error bar on the curve fit and shade region between those two lines. This is what I have tried so far:

import sys
import os
import numpy
import matplotlib.pyplot as plt
from pylab import *
import scipy.optimize as optimization
from scipy.optimize import curve_fit


xdata = numpy.array([-5.6, -5.6, -6.1, -5.0, -3.2, -6.4, -5.2, -4.5, -2.22, -3.30, -6.15])
ydata = numpy.array([-18.40, -17.63, -17.67, -16.80, -14.19, -18.21, -17.10, -17.90, -15.30, -18.90, -18.62])

# Initial guess.
x0     = numpy.array([1.0, 1.0])
#data error
sigma = numpy.array([0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.22, 0.45, 0.35])
sigma1 = numpy.array([0.000001, 0.000001, 0.000001, 0.0000001, 0.0000001, 0.13, 0.22, 0.30, 0.00000001, 1.0, 0.05])

#def func(x, a, b, c):
#    return a + b*x + c*x*x


def line(x, a, b):
    return a * x + b

#print optimization.curve_fit(line, xdata, ydata, x0, sigma)

popt, pcov = curve_fit(line, xdata, ydata, sigma =sigma)

print popt

print "a =", popt[0], "+/-", pcov[0,0]**0.5
print "b =", popt[1], "+/-", pcov[1,1]**0.5

#1 sigma error ######################################################################################
sigma2 = numpy.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])          #make change
popt1, pcov1 = curve_fit(line, xdata, ydata, sigma = sigma2)                           #make change

print popt1

print "a1 =", popt1[0], "+/-", pcov1[0,0]**0.5
print "b1 =", popt1[1], "+/-", pcov1[1,1]**0.5
#####################################################################################################

plt.errorbar(xdata, ydata, yerr=sigma, xerr= sigma1, fmt="none")
plt.ylim(-11.5, -19.5)
plt.xlim(-2, -7)


xfine = np.linspace(-2.0, -7.0, 100)  # define values to plot the function for
plt.plot(xfine, line(xfine, popt[0], popt[1]), 'r-')
plt.plot(xfine, line(xfine, popt1[0], popt1[1]), '--')                                   #make change

plt.show()

但是,我认为我绘制的虚线从我提供的 xdata 和 ydata numpy 数组中获取了一个 sigma 错误,而不是来自曲线拟合.我是否必须知道满足我的拟合曲线的坐标,然后制作第二个数组来制作一个 sigma 误差拟合曲线?

However, I think the dashed line I plotted takes one sigma error from my provided xdata and ydata numpy array, not from the curve fit. Do I have to know the coordinates that satisfy my fit curve and then make a second array to make the one sigma error fit curve?

推荐答案

您似乎在绘制两条完全不同的线.

It seems you are plotting two completely different lines.

相反,您需要绘制三行:第一行是没有任何修正的拟合,其他两行应使用相同的参数 ab 构建,但加上或减去西格玛.您可以从 pcov 中获得的协方差矩阵中获得相应的 sigma.所以你会得到类似的东西:

Instead, you need to plot three lines: the first one is your fit without any corrections, the other two lines should be built with the same parameters a and b, but with added or subtracted sigmas. You obtain the respective sigmas from the covariance matrix you obtain in pcov. So you'll have something like:

y  = line(xfine, popt[0], popt[1])
y1 = line(xfine, popt[0] + pcov[0,0]**0.5, popt[1] - pcov[1,1]**0.5)
y2 = line(xfine, popt[0] - pcov[0,0]**0.5, popt[1] + pcov[1,1]**0.5)

plt.plot(xfine, y, 'r-')
plt.plot(xfine, y1, 'g--')
plt.plot(xfine, y2, 'g--')
plt.fill_between(xfine, y1, y2, facecolor="gray", alpha=0.15)

fill_between 对误差线之间的区域进行阴影处理.

fill_between shades the area between the error bar lines.

结果如下:

如果需要,您可以将相同的技术应用于另一条线路.

You can apply the same technique for your other line if you want.

这篇关于在 scipy 中的曲线拟合线上绘制一个 sigma 误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆