Seaborn-绘制Relplot中的额外移动平均线
我想获得一些有关如何绘制以下Relplot创建的每个子图的移动平均线的建议。我最初尝试使用SNS.LinePlot(),但我无法得到想要的东西。
我打算使用的MA计算IS:
df['avg_price'].rolling('24H', min_periods=360).mean()
到目前为止,我尝试在g.axes.flat 基本图 ax.plot(df.index,df [''' avg_price']。滚动('24H',min_periods = 360).mean(),color ='橙色',label ='ma',lw = 1.5) e或sns.lineplot eres lineplot
都没有工作,所以我想知道我还能做什么?
下面的代码绘制了24个较小的子图,对于每个子图,我想绘制移动平均线。
facet_kws={'sharey': False}
g= sns.relplot(
data=df, x=df.index, y="avg_price", col="customer",
kind="line", col_wrap=4,
color='k', alpha=0.75, facet_kws=facet_kws,
);
g.set_axis_labels("Date", "Average price", fontsize=13);
g.set_titles("{col_name}", size=14);
g.set_xticklabels(rotation=60, fontsize=13);
ax = plt.gca()
ax.xaxis.set_major_locator(md.HourLocator(interval=24))
for ax in g.axes.flat:
ax.axvspan(pd.Timestamp(start_test), pd.Timestamp(end_test), color='y', alpha=0.25, lw=0);
我的数据集的示例: 数据帧看起来像这样:
customer avg_price avg_price2 count1 count2
date_time
2022-06-11 00:00:00 Customer1 4.4656 1.25 36 11084
2022-06-11 00:00:00 Customer2 7.8873 0.92 10 22150
2022-06-11 00:00:00 Customer3 2.3016 1.37 1 2521
2022-06-11 00:00:00 Customer4 3.2421 1.05 221 98973
2022-06-11 00:00:00 Customer5 1.0050 0.94 2 410
... ... ... ... ... ... ...
2022-06-21 10:00:00 Customer1 4.9450 1.99 340 118000
2022-06-21 10:00:00 Customer2 4.0643 2.06 268 20850
2022-06-21 10:00:00 Customer3 3.7034 1.00 25 5100
2022-06-21 10:00:00 Customer4 5.0367 2.64 2098 118251
2022-06-21 10:00:00 Customer5 2.7429 1.57 50 11900
date_time是索引列,基于微小的频率。
有什么建议/示例吗?
编辑:我尝试将MA添加到数据帧本身中,并取得成功( pandas-分配GroupBy滚动均值的结果,为尊重初始dataframe的新列),但是我找不到使用Seaborn使用Seaborn绘制此列的方法。我发现有关转换数据格式的建议,但我不太确定这将如何工作。
I would like to get some suggestions on how to plot the moving average line for each of subplots created by the following relplot. I've initially attempted to use sns.lineplot() but I couldn't get what I wanted.
The MA calculation that I'm planning on using is:
df['avg_price'].rolling('24H', min_periods=360).mean()
So far I attempted to use in for ax in g.axes.flat
basic plot ax.plot(df.index, df['avg_price'].rolling('24H', min_periods=360).mean(), color='orange', label='MA', lw=1.5)
or sns.lineplot
Neither of them worked, so I wondering what else I could do?
The code below plots 24 smaller subplots and for each I would like to plot the moving average line.
facet_kws={'sharey': False}
g= sns.relplot(
data=df, x=df.index, y="avg_price", col="customer",
kind="line", col_wrap=4,
color='k', alpha=0.75, facet_kws=facet_kws,
);
g.set_axis_labels("Date", "Average price", fontsize=13);
g.set_titles("{col_name}", size=14);
g.set_xticklabels(rotation=60, fontsize=13);
ax = plt.gca()
ax.xaxis.set_major_locator(md.HourLocator(interval=24))
for ax in g.axes.flat:
ax.axvspan(pd.Timestamp(start_test), pd.Timestamp(end_test), color='y', alpha=0.25, lw=0);
Example of my dataset:
The dataframe looks like this:
customer avg_price avg_price2 count1 count2
date_time
2022-06-11 00:00:00 Customer1 4.4656 1.25 36 11084
2022-06-11 00:00:00 Customer2 7.8873 0.92 10 22150
2022-06-11 00:00:00 Customer3 2.3016 1.37 1 2521
2022-06-11 00:00:00 Customer4 3.2421 1.05 221 98973
2022-06-11 00:00:00 Customer5 1.0050 0.94 2 410
... ... ... ... ... ... ...
2022-06-21 10:00:00 Customer1 4.9450 1.99 340 118000
2022-06-21 10:00:00 Customer2 4.0643 2.06 268 20850
2022-06-21 10:00:00 Customer3 3.7034 1.00 25 5100
2022-06-21 10:00:00 Customer4 5.0367 2.64 2098 118251
2022-06-21 10:00:00 Customer5 2.7429 1.57 50 11900
Date_time is the index column, minute based frequency.
Any suggestions/examples?
EDIT: I tried to add the MA to the dataframe itself as a column and succeeded (Pandas - assign groupby rolling mean results to new column respecting initial dataframe) but then I couldn't find a way to plot this column using seaborn. I found suggestions regarding transforming the format of the data but I'm not quite sure how this would work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论