我是 python 和绘图新手,我一直在尝试调整刻度标签以显示在垃圾箱下。
本例中我的数据为 5 行:
9.50
11.80
46.68
4.38
30.97
我将其添加到名为 df 的数据框中。
我的代码是:
xLabels = ['0 to 15','15 to 30','30 to 45','45 to 60','60 to 75']
histCurr = df.hist(grid=False, rwidth=0.75, bins=[0, 15, 30, 45, 60, 75], range=[0,75])
histCurr = histCurr[0]
for x in histCurr:
x.spines['right'].set_visible(False)
x.spines['top'].set_visible(False)
x.spines['left'].set_visible(False)
x.tick_params(axis="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="off")
x.set_xlim(0,75)
x.set_xticklabels(xLabels, ha = "center")
标签全部被压扁。
我尝试将 ha 更改为左右以及居中,但这没有帮助。我尝试向 hist 和 xlim 添加范围,但这没有帮助。
如果我不设置 xLabels(注释掉我有 x.set_xticklabels 的行)并运行此命令:
labels = [item.get_text() for item in x.get_xticklabels()]
labels
我得到:
['0', '10', '20', '30', '40', '50', '60', '70', '80']
我在网上找到了一些有关将列表中的项目更改为垃圾箱名称的信息,但这也不是我想要的。
我希望垃圾箱的标 checkout 现在垃圾箱本身的下方。感谢您提前提供的帮助!
更新: 我将代码更改为这样,以帮助处理条形上的百分比,并认为我已经弄清楚了: (来源:https://towardsdatascience.com/advanced-histogram-using-python-bceae288e715)
currDT = df[colNames[currLoc]]
fig, ax = plt.subplots(figsize=(8,8))
counts, bins, patches = ax.hist(currDT, rwidth=0.75, bins=[0, 15, 30, 45, 60, 75])
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.tick_params(axis="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="off")
bin_x_centers = 0.5 * np.diff(bins) + bins[:-1]
ax.set_xticks(bin_x_centers)
ax.set_xticklabels(xLabels)
bin_x_centers = bin_x_centers-2
bin_y_centers = ax.get_yticks()[-2]
for i in range(len(bins)-1):
if counts[i]/counts.sum() != 0:
bin_label = " {0:,.0f}%".format((counts[i]/counts.sum())*100)
else:
bin_label = ""
plt.text(bin_x_centers[i], bin_y_centers, bin_label, rotation_mode='anchor')
最佳答案
这是另一种方法:
pd.cut(df[0],
bins=[0, 15, 30, 45, 60, 75],
labels = ['0 to 15','15 to 30',
'30 to 45','45 to 60',
'60 to 75'])\
.value_counts(sort=False).plot.bar()
输出:
关于python - 如何调整 pandas 直方图上的刻度标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201414/