繁体   English   中英

如何使用Matplotlib中DateTimeIndex的月度数据绘制Yearly系列?

[英]How can I draw Yearly series using monthly data from a DateTimeIndex in Matplotlib?

我在2014年至2018年的一个数据集中拥有6个变量的每月数据。 我正在尝试绘制6个子图(每个变量一个),每个月X轴(1月,2月...),并用其legend绘制5个系列(每年一个)。

这是数据的一部分: 资料预览

我为每个变量(总共30个)创建了5个系列(每年一个),我得到了预期的输出,但是使用了很多行代码。

用更少的代码行来实现此目标的最佳方法是什么?

这是我创建系列的示例:

CL2014 = data_total['Charity Lottery'].where(data_total['Date'].dt.year == 2014)[0:12]

CL2015 = data_total['Charity Lottery'].where(data_total['Date'].dt.year == 2015)[12:24]

这是我如何绘制系列的示例:axCL.plot(xvals,CL2014)

axCL.plot(xvals, CL2015)

axCL.plot(xvals, CL2016)

axCL.plot(xvals, CL2017)

axCL.plot(xvals, CL2018)

我会尝试使用.groupby(),它对于解析如下内容确实非常强大:

for _, group in data_total.groupby([year, month])[[x_variable, y_variable]]:
    plt.plot(group[x_variables], group[y_variables])

因此,在此groupby会将您的data_total DataFrame分为年/月子集,最后使用[[]]解析为x_variable(假设它位于data_total DataFrame中)和y_variable,您可以将它们您感兴趣的那些功能。

我会将您的datetime列分解为单独的year和month列,然后将groupby中的那些新列用作[year,month]。 您也许可以像以前一样通过dt.year和dt.month……不确定,请尝试两种方式!

无需用30个变量来填充名称空间。 Seaborn使这项工作非常容易,但是您需要首先规范化数据框。 这是“规范化”或“无透视”的样子(Seaborn称其为“长格式”):

Date        variable         value
2014-01-01  Charity Lottery  ...
2014-01-01  Racecourse       ...
2014-04-01  Bingo Halls      ...
2014-04-01  Casino           ...

您的屏幕截图是“透视”或“简短格式”数据框。

df_plot = pd.melt(df, id_vars='Date')
df_plot['Year'] = df_plot['Date'].dt.year
df_plot['Month'] = df_plot['Date'].dt.strftime('%b')

import seaborn as sns
plot = sns.catplot(data=df_plot, x='Month', y='value',
                   row='Year', col='variable', kind='bar',
                   sharex=False)
plot.savefig('figure.png', dpi=300)

结果(所有数字都是随机生成的):

海生结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM