简体   繁体   English

您如何将 plot 月份和年份数据转换为 matplotlib 中的条形图?

[英]How do you plot month and year data to bar chart in matplotlib?

I have data like this that I want to plot by month and year using matplotlib.我有这样的数据,我想使用 matplotlib 按月份和年份 plot。

df = pd.DataFrame({'date':['2018-10-01', '2018-10-05', '2018-10-20','2018-10-21','2018-12-06',
                            '2018-12-16', '2018-12-27', '2019-01-08','2019-01-10','2019-01-11',
                            '2019-01-12', '2019-01-13', '2019-01-25', '2019-02-01','2019-02-25', 
                            '2019-04-05','2019-05-05','2018-05-07','2019-05-09','2019-05-10'],
                  'counts':[10,5,6,1,2,
                            5,7,20,30,8,
                            9,1,10,12,50,
                            8,3,10,40,4]})

First, I converted the datetime format, and get the year and month from each date.首先,我转换了日期时间格式,并从每个日期获取年份和月份。

df['date'] = pd.to_datetime(df['date'])

df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month

Then, I tried to do groupby like this.然后,我尝试像这样进行 groupby 。

aggmonth = df.groupby(['year', 'month']).sum()

And I want to visualize it in a barchart or something like that.我想用条形图或类似的方式将其可视化。 But as you notice above, there are missing months in between the data.但正如您在上面注意到的,数据之间缺少几个月。 I want those missing months to be filled with 0s.我希望那些缺失的月份用 0 填充。 I don't know how to do that in a dataframe like this.我不知道如何在这样的 dataframe 中做到这一点。 Previously, I asked this question about filling missing dates in a period of data.之前,我问过这个关于在数据周期中填充缺失日期的问题。 where I converted the dates to period range in month-year format.我将日期转换为月-年格式的期间范围。

by_month = pd.to_datetime(df['date']).dt.to_period('M').value_counts().sort_index()
by_month.index = pd.PeriodIndex(by_month.index)

df_month = by_month.rename_axis('month').reset_index(name='counts')
df_month

idx = pd.period_range(df_month['month'].min(), df_month['month'].max(), freq='M')
s = df_month.set_index('month').reindex(idx, fill_value=0)
s

But when I tried to plot s using matplotlib, it returned an error.但是当我尝试使用 matplotlib 来 plot 时,它返回了一个错误。 It turned out you cannot plot a period data using matplotlib.事实证明,您不能使用 matplotlib 来 plot 周期数据。

So basically I got these two ideas in my head, but both are stuck, and I don't know which one I should keep pursuing to get the result I want.所以基本上我脑子里有这两个想法,但是都卡住了,我不知道我应该继续追求哪一个才能得到我想要的结果。

What is the best way to do this?做这个的最好方式是什么? Thanks.谢谢。

Convert the date column to pandas datetime series, then use groupby on monthly period and aggregate the data using sum , next use DataFrame.resample on the aggregated dataframe to resample using monthly frequency:date列转换为 pandas 日期时间序列,然后在每月period使用groupby并使用sum聚合数据,接下来使用DataFrame.resample对聚合的 Z6A8064B5DF479455500553C47C5505 每月使用频率重新采样 DZDZ

df['date'] = pd.to_datetime(df['date'])
df1 = df.groupby(df['date'].dt.to_period('M')).sum()
df1 = df1.resample('M').asfreq().fillna(0)

Plotting the data:绘制数据:

df1.plot(kind='bar')

在此处输入图像描述

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

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