Matplotlib- plot piecewise Linear Function with three parts

空扰寡人 提交于 2020-01-15 03:27:49

问题


I have some data that I want to fit with a piecewise linear function that has three parts. So something like this if there were two inflection points:

Unfortunately, when I use the following code, I do not get the correct data, it instead looks like this

Anyone know what's wrong? Thanks!

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:


Maybe you want to reread the numpy.piecewise documentation:

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

Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true.

If you want to have 2 function, you need 2 conditions,

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

Example:

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()



来源:https://stackoverflow.com/questions/47358953/matplotlib-plot-piecewise-linear-function-with-three-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!