I have this dataset show below
temp = [0.1, 1, 4, 10, 15, 20, 25, 30, 35, 40]sg =[0.999850, 0.999902, 0.999975, 0.999703, 0.999103, 0.998207, 0.997047, 0.995649, 0.99403, 0.99222]sg_temp = pd.DataFrame({'temp' : temp, 'sg' : sg}) temp sg0 0.1 0.9998501 1.0 0.9999022 4.0 0.9999753 10.0 0.9997034 15.0 0.9991035 20.0 0.9982076 25.0 0.9970477 30.0 0.9956498 35.0 0.9940309 40.0 0.992220I would like to interpolate all the values between 0.1 and 40 on a scale of 0.001 with a spline interpolation and have those points as in the dataframe as well. I have used resample() before but can't seem to find an equivalent for this case.
I have tried this based off of other questions but it doesn't work.
scale = np.linspace(0, 40, 40*1000)interpolation_sg = interpolate.CubicSpline(list(sg_temp.temp), list(sg_temp.sg))2 Answers2
It works very well for me. What exactly does not work for you?Have you correctly used the returned CubicSpline to generate your interpolated values? Or is there some kind of error?
Basically you obtain your interpolated y values by plugging in the new x values (scale) to your returned CubicSpline function:
y = interpolation_sg(scale)I believe this is the issue here. You probably expect that the interpolation function returns you the values, but it returns a function. And you use this function to obtain your values.
If I plot this, I obtain this graph:
import matplotlib.pyplot as pltplt.plot(sg_temp['temp'], sg_temp['sg'], marker='o', ls='') # Plots the originial dataplt.plot(scale, interpolation_sg(scale)) # Plots the interpolated dataComments
Callscale with the result of the interpolation:
from scipy import interpolateout = pd.DataFrame( {'temp': scale, 'sg': interpolate.CubicSpline(sg_temp['temp'], sg_temp['sg'])(scale) })Visual output:
Code for the plot
ax = plt.subplot()out.plot(x='temp', y='sg', label='interpolated', ax=ax)sg_temp.plot(x='temp', y='sg', marker='o', label='sg', ls='', ax=ax)Comments
Explore related questions
See similar questions with these tags.



