将移动平均线拟合成曲线的问题

Problem with fitting Moving average into curve

Python 中的移动平均函数有问题。我尝试使用几种方法,但它们没有用。我之前使用的移动平均线在我的光变曲线上运行良好,但相控图出现问题: Phase diagram with fitted moving average

我不知道为什么它不能正常工作。这是我使用的代码:

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('phase.txt', dtype='float,float', names=["Time","Flux"])
t = data["Time"]
flux = data["Flux"]

def movingaverage(interval, window_size):
   window = np.ones(int(window_size))/float(window_size)
   return np.convolve(interval, window,'same')

av = movingaverage(flux, 10)

plt.plot(t,flux,"C0.", ms=2)
plt.plot(t,av,"r")
plt.show

我希望是这样的: fitted moving average

这是我的文件,其中包含两列值: https://drive.google.com/file/d/1IQScAo8iduv90wuaor8o9eJT85qBpKY4/view?usp=sharing

我注意到您的时间值和相应的通量值没有正确排序。时间值应该是升序的。

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('phase.txt', dtype='float,float', names=["Time","Flux"])
t = data["Time"]
flux = data["Flux"]

# sort time values in ascending order
sort_idx = t.argsort()
t = t[sort_idx]

# sort flux values accordingly
flux = flux[sort_idx]


def movingaverage(interval, window_size):
   window = np.ones(int(window_size))/float(window_size)
   return np.convolve(interval, window,'same')

av = movingaverage(flux, 10)

plt.plot(t,flux,"C0.", ms=2)
plt.plot(t,av,"r")
plt.ylim([0.95, 1.05]) # zoom in a little
plt.show()

这是否会产生您预期的结果?


两种我都试过了,只对时间值进行排序,对时间和通量值进行排序。

仅对时间值进行排序:

排序时间值和通量值:

你觉得哪个更可信?

你的数据比较分散。考虑对更大的 window.

进行平均
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('phase.txt', sep=' ', names=["Time","Flux"]).sort_values(by='Time')
df['MA_10'] = df.Flux.rolling(window=10).mean()
df['MA_100'] = df.Flux.rolling(window=100).mean()

plt.style.use('ggplot')
plt.figure(figsize=(16,9))
plt.scatter(df.Time, df.Flux, c='blue')
plt.plot(df.Time, df.MA_10, c='red')
plt.plot(df.Time, df.MA_100, c='black')
plt.ylim([0.95, 1.05])
plt.show()