I am trying to extrapolate in a loglog plot in python. I did linear regression to fit the data with the best fit curve. Now I want to extend that best fit line to see how the slope goes with an extended range.
My data is really big, so here is a link of my data: my_data
My code looks like this:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
from scipy.optimize import curve_fit
import scipy as sp
import scipy.stats
#########################################################
motl = 'motl.txt'
mx, my = np.loadtxt(motl, unpack=True)
print mx
print my
# now do general curve fit for all data
# Regression Function
def regress(x, y):
#Return a tuple of predicted y values and parameters for linear regression
p = sp.stats.linregress(x, y)
b1, b0, r, p_val, stderr = p
y_pred = sp.polyval([b1, b0], x)
return y_pred, p
# plotting z
allx, ally = mx, my # data, non-transformed
y_pred, _ = regress(np.log(allx), np.log(ally)) # change here # transformed input
plt.loglog(allx, ally, marker='p',color ='g', markersize=3,linestyle='None')
plt.loglog(allx, np.exp(y_pred), "k:") # transformed output
#################################################
# positions to inter/extrapolate
x = np.linspace(12, 14, 1000)
# spline order: 1linear, 2 quadratic, 3 cubic ...
order = 1
# do inter/extrapolation
s = InterpolatedUnivariateSpline(np.log10(mx), np.log10(my), k=order)
y = s(x)
plt.loglog(10**x, 10**y, 'g:')
#######################################################
plt.show()
With regression, the plot looks like the following:
But how do I extrapolate to extend the line from 10^12 to 10^14? your help is appreciated.