-1

I import an excel to python with the Pandas package - this works very well. The excel has two columns - in one column are the "x" values, while in the second column are the "y" values - I would like to plot a cubic spline interpolation, but it doesn't work the way I imagine it:

df = pd.read_excel('test.xls')
f = interp1d(df['x'],df['y '])
plt.plot(f)

where the values are:

x.          y
4,637424    0,00524
4,9027      0,014685
5,082711    0,059966
5,153686    0,124667
5,212604    0,188868
5,227324    0,250883
5,270058    0,319341
5,304338    0,406323
5,315178    0,461895
5,343454    0,82056
5,341876    0,527936
5,4028      0,865394
5,725879    0,989496
5,870601    0,99345
6,497925    0,99956
6,884001    1,00

the y values are probabilities.

  • This is quite trivial issue which can be solved by repeating most simple `interp1d` example. – sivic Nov 05 '20 at 20:43

1 Answers1

1

As shown in the documentation, interp1d returns a function you can use to calculate y-values based on unknown x-values. You cannot plot a function. You have to choose the range of x-values you want, and use it to calculate the y-values, then plot those values together:

f = interp1d(df['x.'], df['y'])
new_x = np.linspace(df['x.'].min(), df['x.'].max(), 100)
new_y = f(new_x)

fig, ax = plt.subplots()
ax.plot(df['x.'], df['y'], 'ro')
ax.plot(new_x, new_y, '-')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • thank you! Is it possible to interpolate it somehow "smooth", that it is like a kind of probability function (i.e. without the knicks) - it doesn't necessarily have to go through the individual points, but a kind of least squares fit or something? – Calculator123 Nov 05 '20 at 20:33
  • That's a different question. Interpolation just gives you the missing values between points. What you want is the best fit. Your data looks pretty much Logistic. See here https://stackoverflow.com/questions/55725139/fit-sigmoid-function-s-shape-curve-to-data-using-python – Diziet Asahi Nov 05 '20 at 22:23