我有两个数组,x
和y
。我想为数组创建自然的三次样条。
我不知道如何精确绘制样条图。
import numpy as np
from scipy.interpolate import CubicSpline
# Calculate 5 natural cubic spline polynomials for 6 points.
# (x,y) = (0,12) (1,14) (2,22) (3,39) (4,58) (5,77)
x = np.array([0, 1, 2, 3, 4, 5 ])
y = np.array([12, 14, 22, 39, 58, 77])
# Calculate natural cubic spline polynomials.
cs = CubicSpline(x, y, bc_type='natural')
答案 0 :(得分:1)
您需要提出一组评估新功能的要点。
import matplotlib.pyplot as plt
# x's at which to evaluate.
xs = np.arange(-1, 10, 0.1)
# Plot data with spline.
plt.plot(x, y, 'o')
plt.plot(xs, cs(xs))
结果是:
答案 1 :(得分:0)
您可以使用matplotlib在您感兴趣的区域上绘制评估的三次样条线
import matplotlib.pyplot as plt
X = np.linspace(-1, 7, 100)
Y = cs(X)
plt.plot(X, Y)
plt.show()