在分组条形图matplotlib中将日期x轴从日更改为月

2024-09-28 21:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在分组条形图中逐日更改x轴刻度。我尝试了here和{a2}和here的解决方案,但没有一个有效。 我的代码显示绘图与每日滴答的工作是如下:

ax = data.groupby(['pos', data['date'].dt.strftime('')])['date'].count().unstack(0).plot.bar(title = 'TITLE', figsize=(14,8))
_ = ax.set_xlabel('day')
_ = ax.set_ylabel('count')
mylabels = ['R', 'L']
_ = ax.legend(labels=mylabels)
plt.show()

daily plot 我尝试了以下方法,但没有成功(但也没有抛出错误):

^{pr2}$

Tags: 代码posa2绘图datadateherecount
1条回答
网友
1楼 · 发布于 2024-09-28 21:57:26

我是怎么做到的:

grouped_data = data \
.groupby(['pos', data['date'].dt.strftime('')])['date'] \
.count() \
.unstack(0) \
.reset_index()
grouped_data.columns = ['date', 'r_count', 'l_count']
grouped_data['date'] = pd.to_datetime(grouped_data['date'])

fig, ax = plt.subplots(figsize=(14, 8))
ax.bar(grouped_data['date'], grouped_data['r_count'])
ax.bar(grouped_data['date'], grouped_data['l_count'])
ax.xaxis_date()
ax.set_xlabel('date')
ax.set_ylabel('count')
mylabels = ['r', 'l']
ax.legend(labels=mylabels)
plt.show()

plot

相关问题 更多 >