在您的特定情况下,您还可以尝试将np.linspace函数的最后一个参数更改为较小的数字np.linspace(x[0], x[-1], 10)。
演示代码:import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
data = np.random.rand(100,2)
tempx = list(data[:, 0])
tempy = list(data[:, 1])
x = np.array(sorted([point*10 + tempx.index(point) for point in tempx]))
y = np.array([point*10 + tempy.index(point) for point in tempy])
x_int = np.linspace(x[0], x[-1], 10)
tck = interpolate.splrep(x, y, k = 3, s = 1)
y_int = interpolate.splev(x_int, tck, der = 0)
fig = plt.figure(figsize = (5.15,5.15))
plt.subplot(111)
plt.plot(x, y, marker = 'o', linestyle='')
plt.plot(x_int, y_int, linestyle = '-', linewidth = 0.75, color='k')
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
你也可以用熊猫的滚动平均值来平滑数据:import pandas as pd
data = [...(your data here)...]
smoothendData = pd.rolling_mean(data,5)
滚动平均的第二个参数是移动平均(滚动平均)周期。也可以将数据“data.reverse”反转,以这种方式获取数据的滚动平均值,并将其与前向滚动平均值组合。另一种选择是指数加权移动平均:
Pandas: Exponential smoothing function for column