0

I have the following script that plots a graph:

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)
plt.show()

The problem is, that the line goes straight from point to point, but I want to smooth the line between the points. enter image description here

If I use scipy.interpolate.spline to smooth my data I got following result:

 order = np.array([0,1,2])
 y = np.array([5, 4.31, 4.01])
 xnew = np.linspace(order.min(), order.max(), 300)
 smooth = spline(order, y, xnew)
 plt.plot(xnew, smooth)
 plt.show()

enter image description here

But I want to have the same result like in that given example

lazzat
  • 31
  • 10
  • if you know the underlying correlation (e.g. exponential decay or whatsoever), you could fit the function to the data using `scipy.optimize.curvefit`. – Moritz Jul 21 '18 at 15:45

1 Answers1

1

If you use more points than 3 you will get the same result as in the linked question. There are many ways a spline of order 3 can go through 3 points.

But you may of course reduce the order to 2.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)

xnew = np.linspace(x.min(), x.max(), 300)
smooth = spline(x, y, xnew, order=2)
plt.plot(xnew, smooth)


plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712