将平均移动平均曲线安装到曲线的问题

发布于 2025-01-29 21:29:46 字数 964 浏览 3 评论 0原文

我在Python中的移动平均功能有问题。我尝试使用几种方法,但它们没有起作用。我在光线曲线上使用良好工作之前使用的移动平均值,但分阶段发生问题: 具有拟合移动平均值的相图

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

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

我希望这样的东西: 拟合的移动平均线

这是我的文件,其中有两个列中的值: bpky4/查看?usp =共享

I have a problem with the Moving average function in Python. I tried using a couple of methods but they didn't work. Moving average that I used before work well on my light curve, but problem occurs with phased diagram:
Phase diagram with fitted moving average

I don't know why it's not working properly. Here is a code I use:

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

I would expect something like this:
fitted moving average

Here is my file with values in two columns:
https://drive.google.com/file/d/1IQScAo8iduv90wuaor8o9eJT85qBpKY4/view?usp=sharing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2025-02-05 21:29:46

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

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()

这会产生您期望的结果吗?


我曾尝试过同时对时间值进行排序以及分类时间和磁通值。

仅分类时间值:

排序时间值和磁通值:

您看起来更合理?

I noticed that your time values and the corresponding flux values are not properly sorted. Time values should be ascending.

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()

Does this yield the result you were expecting?


I have tried both, only sorting time values as well as sorting time and flux values.

Only sorting time values:
![enter image description here

Sorting time values and flux values:
![enter image description here

Which one looks more plausible to you?

说好的呢 2025-02-05 21:29:46

您的数据非常分散。考虑在较大的窗口上平均。

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()

Your data is quite scattered. Consider averaging over a larger 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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文