如何调整熊猫直方图上的刻度标签?

时间:2019-12-05 18:40:30

标签: python pandas plot histogram

我是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")

所有标签都被压扁了。

Click here for image

我尝试将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')

1 个答案:

答案 0 :(得分:0)

这是另一种方式:

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()

输出:

enter image description here