Matplotlib-绘制分段线性函数有三个部分

时间:2017-11-17 20:42:14

标签: python matplotlib data-fitting piecewise

我有一些数据我希望适合具有三个部分的分段线性函数。如果有两个拐点,那就是这样的事情:

enter image description here

不幸的是,当我使用下面的代码时,我没有得到正确的数据,而是看起来像这样

enter image description here

任何人都知道什么是错的?谢谢!

def piecewise_linear2(x, x0, y0, k1, k2, k3):
    return np.piecewise(x, [x < x0], [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0, lambda x:k3*x + y0-k3*x0])

fit_days = np.array([1786,1852,2067,2142,2143,2313,2320,2426,2550,2681,2685,3037,3109,3178,3436,3485,3512,3914,4013,4016,4220,4268,4372,4577,4584,4966,5011,5387,5748,5810,6003,6129,6170,6283,6605,6693,6973,7085,7228,7620,7730,7901,8139,8370,8448,8737,8824,9089,9233,9321,9509,9568,9642,9756,9915,10601,10942], dtype=np.float)
fit_expt= np.array([.6,.62,.62,.65,.64,.63,.67,.69,.64,.67,.66,.67,.64,.685,.705,.707,.708,.694,.754,.745,.729,.736,.727,.757,.747,.764,.775,.79,.811,.815,.815,.833,.831,.829,.843,.858,.880,.872,.874,.893,.8905,.8916,.9095,.9142,.9109,.9185,.9169,.9251,.9290,.9304,.9467,.9378,0.9464,0.9508,0.9583,0.9857,0.9975],dtype=np.float)

xr2= fit_days[26:57]
yr2= fit_expt[26:57]
p0 = [np.mean(xr2), np.mean(yr2), 1, 1,1]

p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2, p0)
x = np.linspace((xr2[0]-100), 11000, 3000)
#p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2)
xd = np.linspace(0, 19, 11000)
plt.plot(xr2, yr2, "o",color="#2ca02c")
plt.plot(x, piecewise_linear2(x, *p),linestyle='dashed',color="#2ca02c")

1 个答案:

答案 0 :(得分:1)

也许您想重新阅读numpy.piecewise文档:

  

numpy.piecewise(x, condlist, funclist, *args, **kw)

     

给定一组条件和相应的函数,在条件为真的任何地方评估输入数据的每个函数。

如果你想拥有2个功能,你需要2个条件,

np.piecewise(x, [x < x0, x>= x0],  [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0])

示例:

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as optimize
def piecewise_linear2(x, x0, y0, k1, k2):
    return np.piecewise(x, [x < x0, x>= x0], 
                        [lambda x:k1*x + y0-k1*x0, 
                         lambda x:k2*x + y0-k2*x0])

fit_days = np.array([1786,1852,2067,2142,2143,2313,2320,2426,2550,2681,2685,3037,3109,3178,3436,3485,3512,3914,4013,4016,4220,4268,4372,4577,4584,4966,5011,5387,5748,5810,6003,6129,6170,6283,6605,6693,6973,7085,7228,7620,7730,7901,8139,8370,8448,8737,8824,9089,9233,9321,9509,9568,9642,9756,9915,10601,10942], dtype=np.float)
fit_expt= np.array([.6,.62,.62,.65,.64,.63,.67,.69,.64,.67,.66,.67,.64,.685,.705,.707,.708,.694,.754,.745,.729,.736,.727,.757,.747,.764,.775,.79,.811,.815,.815,.833,.831,.829,.843,.858,.880,.872,.874,.893,.8905,.8916,.9095,.9142,.9109,.9185,.9169,.9251,.9290,.9304,.9467,.9378,0.9464,0.9508,0.9583,0.9857,0.9975],dtype=np.float)

xr2= fit_days[26:57]
yr2= fit_expt[26:57]
p0 = [np.mean(xr2), np.mean(yr2), 1, 1]

p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2, p0)
x = np.linspace((xr2[0]-100), 11000, 3000)
#p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2)
xd = np.linspace(0, 19, 11000)
plt.plot(xr2, yr2, "o",color="#2ca02c")
plt.plot(x, piecewise_linear2(x, *p),linestyle='dashed',color="#2ca02c")

plt.show()

enter image description here