gpt4 book ai didi

python - 使用 matplotlib 缺少第一个 x 标签

转载 作者:行者123 更新时间:2023-11-28 18:09:41 24 4
gpt4 key购买 nike

这是我的数据:

a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'],
'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]})

这是我绘制条形图的代码:

fig, ax = plt.subplots()
ind = np.arange(a3.loc[:,'OfficeName'].nunique()) # the x locations for the groups
width = 0.35 # the width of the bars
p1 = ax.bar(ind,a3.loc[:,'Ratio'],width)
ax.set_title('Ratio of refunds to purchases')
ax.set_xticklabels(a3.loc[:,'OfficeName'],ha='center')
#ax.set_xticklabels(['a','b','c','d','e','f','g','h'],ha='center')
ax.set_xlabel('x Group')
ax.set_ylabel('Ratio')
plt.show()

但是,在我的图表中缺少第一个 x 标签:

enter image description here

我觉得这个问题和Matplotlib: Move x-axis tick labels one position to left不一样因为我什至不需要旋转 x 标签。

谁能解释为什么会发生这种情况以及如何解决?

最佳答案

有一种更简单的方法可以在 Pandas 中生成条形图:

a3.set_index('OfficeName').plot.bar(width=0.35, legend=False)
plt.xticks(rotation=0, ha='center')

(你仍然需要设置x和y轴标签和标题。)

关于python - 使用 matplotlib 缺少第一个 x 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com