显示分组条形图Python的计数和百分比标签 [英] Show Count and percentage labels for grouped bar chart python

查看:419
本文介绍了显示分组条形图Python的计数和百分比标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   age_group   Mis   surv   unk   death  total  surv_pct  death_pct
 0      0-9     1     2     0     3       6       100.0       0.0 
 1    10-19     2     1     0     1       4        99.9       0.0
 2    20-29     0     3     0     1       4        99.9       0.0
 3    30-39     0     7     1     2      10       100.0       0.0
`4    40-49     0     5     0     1       6        99.7       0.3
 5    50-59     0     6     0     4      10        99.3       0.3
 6    60-69     0     7     1     4      12        98.0       2.0
 7    70-79     1     8     2     5      16        92.0       8.0       
 8    80+       0    10     0     7      17        81.0      19.0

并且图表看起来像这样

And The chart looks something like this

我使用以下代码创建了图表:

I created the chart with this code:

ax = df.plot(y=['deaths', 'surv'],
             kind='barh',
             figsize=(20,9),
             rot=0,
             title= '\n\n surv and deaths by age group')

ax.legend(['Deaths', 'Survivals']);
ax.set_xlabel('\nCount');
ax.set_ylabel('Age Group\n');


如何将计数和百分比标签添加到分组的条形图中?我希望它看起来像这张图


How could I add count and percentage labels to the grouped bars? I would like it to look something like this chart

推荐答案

由于没有其他人提出任何建议,因此这是一种使用数据框结构进行处理的方法.

Since nobody else has suggested anything, here is one way to approach it with your dataframe structure.

from matplotlib import pyplot as plt
import pandas as pd

df = pd.read_csv("test.txt", delim_whitespace=True)

cat = ['death', 'surv']

ax = df.plot(y=cat,
             kind='barh',
             figsize=(20, 9),
             rot=0,
             title= '\n\n surv and deaths by age group')

#making space for the annotation
xmin, xmax = ax.get_xlim()
ax.set_xlim(xmin, 1.05 * xmax)

#connecting bar series with df columns
for cont, col in zip(ax.containers, cat):
    #connecting each bar of the series with its absolute and relative values 
    for rect, vals, perc in zip(cont.patches, df[col], df[col+"_pct"]):
        #annotating each bar
        ax.annotate(f"{vals} ({perc:.1f}%)", (rect.get_width(), rect.get_y() + rect.get_height() / 2.),
                     ha='left', va='center', fontsize=10, color='black', xytext=(3, 0),
                     textcoords='offset points')

ax.set_yticklabels(df.age_group)
ax.set_xlabel('\nCount')
ax.set_ylabel('Age Group\n')
ax.legend(['Deaths', 'Survivals'], loc="lower right")
plt.show()

样本输出:

如果每个类别的百分比相加,则还可以即时计算百分比.这样就不必使百分比列具有完全相同的名称结构.另一个问题是注释的字体大小,用于标注最大条形的空间的缩放比例以及条形与注释之间的距离不是交互式的,可能需要进行微调.
但是,我不喜欢将熊猫和matplotlib绘图功能混合使用.在某些情况下,大熊猫的轴定义会干扰matplotlib和datetime对象……嗯,让我们不要谈论它.

If the percentages per category add up, one could also calculate the percentages on the fly. This would then not necessitate that the percentage columns have exactly the same name structure. Another problem is that the font size of the annotation, the scaling to make space for labeling the largest bar, and the distance between bar and annotation are not interactive and may need fine-tuning.
However, I am not fond of this mixing of pandas and matplotlib plotting functions. I had cases where the axis definition by pandas interfered with matplotlib, and datetime objects ... well, let's not talk about that.

这篇关于显示分组条形图Python的计数和百分比标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆