我正在尝试使用上面的x,y坐标来绘制平滑曲线。无论我得到的图表是否超出了我的数据范围。我的代码片段就在这里。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline
ylist = [0.36758563074352546, 0.27634194831013914, 0.22261484098939929, 0.10891089108910891, 0.31578947368421051, 0.086956521739130432, 0.27272727272727271, 0.18181818181818182, 0.0, 0.0, 0.92000000000000004, 0.0, 0.10526315789473684, 0.23333333333333334]
xlist = [0.025000000000000001, 0.075000000000000011, 0.125, 0.17500000000000002, 0.22500000000000001, 0.27500000000000002, 0.32500000000000001, 0.375, 0.42500000000000004, 0.47500000000000003, 0.52500000000000002, 0.57500000000000007, 0.625, 0.97500000000000009]
xlist_smooth = np.linspace(xlist.min(), xlist.max(), 100)
ylist_smooth = spline(xlist, ylist, xlist_smooth)
plt.plot(xlist_smooth,ylist_smooth)
我得到以下曲线作为输出
答案 0 :(得分:2)
我认为这里的问题是更高阶的样条插值不适合平滑数据。
下面我绘制了0到3阶的样条插值。你看到的是,一旦你要求导数的连续性(阶数2和更高),你会遇到最后两点的问题。
我想在这里选择样条插值不是一个好选择。插值假定没有测量误差,并且您的数据中似乎有明显的异常值。
根据您要在此处执行的操作,拟合分段连续样条(order = 1)可能适合您。否则你可能需要寻找不同的平滑策略。
import numpy as np
from scipy.interpolate import spline
import matplotlib.pyplot as plt
ylist = [0.36758563074352546, 0.27634194831013914, 0.22261484098939929,
0.10891089108910891, 0.31578947368421051, 0.086956521739130432,
0.27272727272727271, 0.18181818181818182, 0.0, 0.0,
0.92000000000000004, 0.0, 0.10526315789473684, 0.23333333333333334]
xlist = [0.025000000000000001, 0.075000000000000011, 0.125, 0.17500000000000002,
0.22500000000000001, 0.27500000000000002, 0.32500000000000001, 0.375,
0.42500000000000004, 0.47500000000000003, 0.52500000000000002,
0.57500000000000007, 0.625, 0.97500000000000009]
xlist_smooth = np.linspace(min(xlist), max(xlist), 100)
fig, axes = plt.subplots(4,1, sharex=True)
for order, ax in enumerate(axes):
ylist_smooth = spline(xlist, ylist, xlist_smooth, order=order)
ax.plot(xlist_smooth, ylist_smooth, label="spline order %s" % order)
ax.scatter(xlist, ylist, label="knots")
ax.set_ylim(min(xlist)-1,max(xlist)+1)
ax.legend()