lazzat
lazzat

Reputation: 31

interpolate curve between three values

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

Upvotes: 0

Views: 2642

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339250

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

Upvotes: 1

Related Questions