python - 如何使用多个自定义值标记条形

标签 python matplotlib seaborn bar-chart plot-annotations

我有这个数据框

rules count    percentage    groups   weight

A      15      24%            1       10
B      5       2%             2       30
C      25      50%            3       50

我有以下代码:

sns.set(rc={'figure.figsize':(18,9.5)})
plots = sns.barplot(x="rules", y="count", data=df, hue=df['groups'], dodge=False)
percentage = df['percentage'].tolist()
weight = df['weight'].tolist()
patches = plots.patches
for i in range(len(patches)):
   x = patches[i].get_x() + patches[i].get_width()/2
   y = patches[i].get_height()+.09
   plots.annotate('{:.1f}%'.format(percentage[i]), (x, y), ha='center',  va='bottom', size=14)
   plots.annotate('{:.0f}'.format(weight[i]), (x, y), ha='center', va='top', color='white', size=15, fontweight="bold")

尝试用百分比注释条形图时出现以下错误。

条形图中是另一个数字,对应于 df 中的权重列。

IndexError                                Traceback (most recent call last)
<ipython-input-120-0ef14f891711> in <module>()
      7    x = patches[i].get_x() + patches[i].get_width()/2
      8    y = patches[i].get_height()+.09
----> 9    plots.annotate('{:.1f}%'.format(percentage[i]), (x, y), ha='center',  va='bottom', size=14)
     10    plots.annotate('{:.0f}'.format(weight[i]), (x, y), ha='center', va='top', color='white', size=15, fontweight="bold")
     11 plots.set_xticklabels(plots.get_xticklabels(), rotation=90, fontsize=15)

IndexError: list index out of range

最佳答案

import pandas as pd
import seaborn as sns

# test data
data = {'rules': ['A', 'B', 'C'], 'count': [15, 5, 25],
        'percentage': ['24%', '2%', '50%'], 'groups': [1, 2, 3], 'weight': [10, 30, 50]}
df = pd.DataFrame(data)

# plot
ax = sns.barplot(x="rules", y="count", data=df, hue='groups', dodge=False)

# since you are using hue, there are multiple containers
for c in ax.containers:
    # set the bar label based on the y-axis
    ax.bar_label(c, label_type='center', padding=1)
    # add an annotations with custom labels
    ax.bar_label(c, labels=df.percentage, label_type='edge', padding=1)

# pad the spacing between the number and the edge of the figure 
ax.margins(y=0.1)

enter image description here


  • 带有两个自定义标签的注释('weight''percentage')。
ax = sns.barplot(x="rules", y="count", data=df, hue='groups', dodge=False)

# since you are using hue, there are multiple containers
for c in ax.containers:
    # add an annotations with custom labels
    ax.bar_label(c, labels=df.weight, label_type='center')
    # add an annotations with custom labels
    ax.bar_label(c, labels=df.percentage, label_type='edge', padding=1)
    
ax.margins(y=0.1)

enter image description here


  • 现有代码的问题是有 9 个补丁,如 print(patches)list(patches) 所示,可以通过选择解决仅高度大于 0 的补丁。
    • 出现这种情况是因为正在使用 hue,但对于 'rules' 中的每个值,'groups' 中只有一个值。
plots = sns.barplot(x="rules", y="count", data=df, hue=df['groups'], dodge=False)
percentage = df['percentage'].tolist()
weight = df['weight'].tolist()
patches = plots.patches

# select patches with a height > 0
patches = [p for p in patches if p.get_height() > 0]

for i, p in enumerate(patches):
    
    x = p.get_x() + patches[i].get_width()/2
    y = p.get_height()+.09
    
    plots.annotate(f'{percentage[i]}', (x, y), ha='center',  va='bottom', size=14)
    plots.annotate(f'{weight[i]:.0f}', (x, y), ha='center', va='top', color='white', size=15, fontweight="bold")

enter image description here

关于python - 如何使用多个自定义值标记条形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72970649/

相关文章:

python - 如何在 Seaborn 图中显示标签(没有找到带有标签的句柄放在图例中。)?

python - Seaborn Lineplot 显示自定义中心线而不是均值

python - Twisted + SQLAlchemy 和最好的方法

python - 如何重新格式化一串字符串操作并获取其结果

python - 错误 : Cannot uninstall requests 2. 25.1,未找到记录文件。提示:该软件包是通过 rpm 安装的

python - 使用matplotlib时对图的控制

python - 将 Spyder IDE 图形窗口置于前面

python - 导入无显示的 Matplotlib

python - 尽管两个查询集在 shell 中打印出相同的内容,但 Django 的 assertQuerysetEqual() 方法还是失败了?

python - 条形图绘制总和,而不是列的平均值