Pandasmatplotlib xaxis仅每隔五个位置添加一个标签

2024-10-03 02:35:42 发布

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

我正在matplotlib中绘制一个条形图,其中包括从具有相同x轴的3个不同数据帧中的一列中获取数据,并将条形图并排绘制

它主要是工作除了x轴只标签每五个位置,而不是所有的位置?如何标记每个位置

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(25,10))

plt.subplots_adjust(hspace=0.8, wspace=0.2)


x = df_rightmoveSmry.index

labels = list(df_rightmoveSmry['Adj Date'].astype(str).str[0:10])

y1 = df_rightmoveSmry['branches']
y2 = df_zooplaSmry['branches']
y3 = df_onthemarketSmry['branches']
ax.set_title('Number of estate agents on each Platform', fontsize=20)
ax.set_xticklabels(labels, rotation = 90, fontsize=20)
ax.yaxis.set_tick_params(labelsize=15)
ax.bar(x, y1, width=0.3)
ax.bar(x+0.3, y2, width=0.3)
ax.bar(x+0.6, y3, width=0.3)

提供:

enter image description here


Tags: dflabels绘制barpltaxwidth条形图
1条回答
网友
1楼 · 发布于 2024-10-03 02:35:42

或许可以试试:

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(25,10))

plt.subplots_adjust(hspace=0.8, wspace=0.2)


x = df_rightmoveSmry.index

labels = list(df_rightmoveSmry['Adj Date'].astype(str).str[0:10])

y1 = df_rightmoveSmry['branches']
y2 = df_zooplaSmry['branches']
y3 = df_onthemarketSmry['branches']
ax.set_title('Number of estate agents on each Platform', fontsize=20)
ax.set_xticks(list(df_rightmoveSmry['Adj Date'].unique())) # < - this line added
ax.set_xticklabels(labels, rotation = 90, fontsize=20)
ax.yaxis.set_tick_params(labelsize=15)
ax.bar(x, y1, width=0.3)
ax.bar(x+0.3, y2, width=0.3)
ax.bar(x+0.6, y3, width=0.3)

相关问题 更多 >