# Matplotlib 绘制平滑曲线

## 使用 `scipy.interpolate.make_interp_spline()` 类绘制平滑曲线

``````import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])

model=make_interp_spline(x, y)

xs=np.linspace(1,7,500)
ys=model(xs)

plt.plot(xs, ys)
plt.title("Smooth Spline Curve")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
``````

``````import numpy as np
import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])

plt.plot(x, y)
plt.title("Curve plotted using the given points")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
``````

## 使用 `scipy.ndimage.gaussian_filter1d()` 类绘制平滑曲线

``````import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter1d

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])
y_smoothed = gaussian_filter1d(y, sigma=5)

plt.plot(x, y_smoothed)
plt.title("Spline Curve Using the Gaussian Smoothing")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
``````

`sigma` 参数代表高斯核的标准差，增加 `sigma` 的值会得到更平滑的曲线。

## 使用 `scipy.interpolate.interp1d` 类绘制平滑曲线

``````import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])

cubic_interploation_model=interp1d(x,y,kind="cubic")
xs=np.linspace(1,7,500)
ys=cubic_interploation_model(xs)

plt.plot(xs, ys)
plt.title("Spline Curve Using Cubic Interpolation")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
``````