简体   繁体   中英

Python multiple 2D polynomial fits

I use IPython Notebook app. since half a year for my master thesis in astrophysics. It's my first time asking a question here but many times I have benefited from stack overflow. So, I already searched the internet and the stack overflow database but didn't find the solution to my problem.

I have a plot of data points which form a curve with two 'breaks' as it changes its inclination. Now, I want to plot at least two fits of first order into this plot according to the breaks in the curve.

I already know how to fit one polynomial of first order, but have no idea how one could define the fit ranges. I mean I want to tell Python make one fit for the data points in the range [0,15] and one for the datapoints in [15, 30].

My code looks like this:

plt.subplot(1,2,1)

scatter(res1_c18o[:,4], res1_c18o[:,3], c= res1_c18o[:,2], s=10)

plt.colorbar()

x= res1_c18o[:,4]

y=res1_c18o[:,3]

p = np.polyfit(x,y, 1)

pl.plot(x,p[0]*x+p[1])

Like mentioned above this builds one fit for the whole curve. This is the last thing I need to do for my master thesis. These are in fact the last plots of the last chapter ;)

Please share your ideas with me, I would appreciate any help!

Thank you very much, Betty

Choose first your fit range, with x in [0,15] .

import numpy as np
xrange = x[np.logical_and(x>=0,x<=15)]
yrange = y[np.logical_and(x>=0,x<=15)]

p = np.polyfit(xrange,yrange, 1)

#plot the fit only in its range of validity:
plt.plot(xrange,p[0]*xrange+p[1])

You can do the same with the second range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM