Pandas 栏如何标记期望值

Pandas bar how to label desired values

d = {'X':[1,2,3,4],'A': [50,40,20,60], '% of Total in A':[29.4,23.5,11.8,35.3] , 'B': [25,10,5,15], '% in A' :[50,25,25,25]}
df = pd.DataFrame(d)
ax = df.plot(x='X',y="A", kind="bar")
df.plot(x='X', y="B", kind="bar", ax=ax,color='C2')

   X   A  % of Total in A   B  % in A
0  1  50             29.4  25      50
1  2  40             23.5  10      25
2  3  20             11.8   5      25
3  4  60             35.3  15      25

我有上面的数据框,我知道如何根据两列 A 和 B 绘制堆积条形图。

如何在条形顶部添加值标签,例如对于 X=0,我想在蓝色条上方标记 50(总数的 29.4%),在蓝色条上方标记 25(总数的 50%)蓝条内的绿条。

感谢任何帮助。

第一个柱存储在 ax.containers[0] 中,第二个柱存储在 ax.containers[1] 中。您可以使用这些容器以及相应标签的列表来调用 ax.bar_label(...)

顺便说一下,您在第二个条形图中缺少 x=

from matplotlib import pyplot as plt
import pandas as pd

d = {'X': [1, 2, 3, 4], 'A': [50, 40, 20, 60], '% of Total in A': [29.4, 23.5, 11.8, 35.3], 'B': [25, 10, 5, 15], '% in A': [50, 25, 25, 25]}
df = pd.DataFrame(d)
ax = df.plot(x='X', y="A", kind="bar")
df.plot(x='X', y="B", kind="bar", color='C2', ax=ax)

ax.bar_label(ax.containers[0], labels=df['% of Total in A'])
ax.bar_label(ax.containers[1], labels=df['% in A'], color='white')
plt.show()

为了进一步强调 BA 的一部分,您可以给它们相同的颜色,并用阴影线 B。例如:

ax = df.plot(x='X', y="A", kind="bar", color='dodgerblue')
df.plot(x='X', y="B", kind="bar", facecolor='dodgerblue', hatch='xx', rot=0, ax=ax)

ax.bar_label(ax.containers[0], labels=[f'{p} %' for p in df['% of Total in A']])
ax.bar_label(ax.containers[1], labels=[f'{p} %' for p in df['% in A']], color='white')
for spine in ['top', 'right']:
    ax.spines[spine].set_visible(False)

  • 条形图未正确堆叠。补丁按 z 顺序堆叠,而不是垂直(y 顺序)。x 轴也不正确,因为第二个图中缺少 x='X'
  • 使用zip组合containerscols,然后将自定义labels传递给labels=参数。
  • 另见 , and Adding value labels on a matplotlib bar chart for a thorough explanation about .bar_label
ax = df.plot(kind='bar', x='X', y=['A', 'B'], stacked=True, rot=0, color=['tab:blue', 'tab:green'])
ax.legend(bbox_to_anchor=(1, 1.02), loc='upper left')

# specify the columns to uses for alternate labels, in order based on the order of y=
cols = ['% of Total in A', '% in A']

for c, col in zip(ax.containers, cols):
    
    labels = df[col]

    # Use the alternate column for the labels instead of the bar height (or width of horizontal bars)
    labels = [f'{v}%' for v in labels]
    
    # remove the labels parameter if it's not needed for customized labels
    ax.bar_label(c, labels=labels, label_type='edge')

ax.margins(y=0.1)