【发布时间】:2019-07-19 02:04:27
【问题描述】:
我正在尝试绘制从 WhatsApp 聊天中提取的平均长度和消息数量。 Data 数据框包含以下列:
-
Date&Time,熊猫 DateTime 对象; -
msg,实际消息; -
name,发信人; -
msg_len,留言字数
使用下面的代码,我创建了一个堆叠图,其中 X 轴上有按月份分组的消息。我发现如果在一个月内没有交换消息,则该月的情节跳过,我用 0 条消息显示该月的内容,然后我绘制计数或平均值。如何修改我的代码?
Data = pd.DataFrame('./Wappmsg.txt')
Data['Date&Time']=pd.to_datetime(Data['Date&Time'], dayfirst=True, infer_datetime_format=True)
fig,axes = plt.subplots(2,1,
figsize=(18,10),
sharex = True)
group_by_month_per_user = Data.groupby([Data['Date&Time'].dt.strftime('%Y-%m'), 'name']).count().unstack()
group_by_month_per_user['msg_len'].plot(kind='bar', stacked=True, legend=['name'], ax = axes[0])
axes[0].set_title('Number of text per month')
axes[0].set_ylabel('Count')
group_by_month_per_user = Data.groupby([Data['Date&Time'].dt.strftime('%Y-%m'), 'name']).mean().unstack()
group_by_month_per_user['msg_len'].plot(kind='bar', stacked=True, legend=['name'], ax = axes[1])
axes[1].set_title('Mean lenght of a message per month')
axes[1].set_ylabel('Mean lenght')
axes[1].set_xlabel('Year-Month')
axes[1].legend()
plt.xticks(rotation=90)
plt.show()
【问题讨论】:
标签: python pandas datetime matplotlib plot