interpolate curve between three values

戏子无情 提交于 2021-01-29 12:42:24

问题


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.

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()

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


回答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()



来源:https://stackoverflow.com/questions/51457342/interpolate-curve-between-three-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!