python使曲线变得平滑_如何在Python中平滑数据?
在您的特定情況下,您還可以嘗試將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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python使曲线变得平滑_如何在Python中平滑数据?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 知乎接口_Python采用
- 下一篇: python元胞转list_[Pytho