pav*_*pav 5 python plot histogram pandas
我对 python 和绘图很陌生,我一直在尝试调整刻度标签以显示在垃圾箱下方。
本例中我的数据为 5 行:
9.50
11.80
46.68
4.38
30.97
Run Code Online (Sandbox Code Playgroud)
我将其添加到名为 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")
Run Code Online (Sandbox Code Playgroud)
标签看起来全部被压扁在左边。
我尝试过将 ha 更改为右、左以及中心,但这没有帮助。我尝试向 hist 和 xlim 添加范围,但这没有帮助。
如果我不设置 xLabels(注释掉我有 x.set_xticklabels 的行)并运行以下命令:
labels = [item.get_text() for item in x.get_xticklabels()]
labels
Run Code Online (Sandbox Code Playgroud)
我得到:
['0', '10', '20', '30', '40', '50', '60', '70', '80']
Run Code Online (Sandbox Code Playgroud)
我在网上找到了一些有关将列表中的项目更改为垃圾箱名称的信息,但这也不是我想要的。
我希望垃圾箱的标签出现在垃圾箱本身的下方。感谢您提前的帮助!
更新:我将代码更改为此,以帮助处理条形上的百分比,并认为我已经弄清楚了:(来源: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')
Run Code Online (Sandbox Code Playgroud)
这是另一种方法:
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()
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
7945 次 |
| 最近记录: |