【发布时间】:2020-03-30 18:06:59
【问题描述】:
我是 python 和绘图的新手,我一直在尝试调整刻度标签以显示在 bin 下。
对于这个例子,我的数据是 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']
我在网上找到了一些关于将列表中的项目更改为 bin 名称的内容,但这也不是我想要的。
我希望垃圾箱的标签出现在垃圾箱本身的下方。提前感谢您的帮助!
更新: 我将代码更改为此以帮助计算条形上的百分比,并认为我想通了: (来源: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')
【问题讨论】:
标签: python pandas plot histogram