python - 在 scipy 中的曲线拟合线上绘制一个西格玛误差条

标签 python optimization plot scipy

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

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

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 错误,而不是来自曲线拟合。我是否必须知道满足我的拟合曲线的坐标,然后制作第二个数组来制作 one sigma 误差拟合曲线?

enter image description here

最佳答案

看来您正在绘制两条完全不同的线。

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

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 为误差线之间的区域添加阴影。

这是结果:

enter image description here

如果需要,您可以对其他线路应用相同的技术。

关于python - 在 scipy 中的曲线拟合线上绘制一个西格玛误差条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658190/

相关文章:

python - 在 Python 中导入函数

swift - 在 swift 中快速添加字符串

sql - WHERE 子句中的 "Conditional Conditions"(应用哪个条件取决于 "mode"标志)

r - 在具有不同数据框的 ggplot 中显示多个图例

python :Read from a USB HID device

python - 在 python 中的 Cypher 查询中传递变量

python - Pandas Python 中的共享 x 轴

python - 在 Cython 中优化代码的技巧

c - 在 Xcode 中编译程序后,如何绘制输出数据?

c++ - 如何在 C++ 中制作类似于 MATLAB 的绘图库?