仅在堆叠条形顶部添加总值

发布于 2025-01-10 22:53:00 字数 1274 浏览 2 评论 0原文

我正在使用以下条形图:

Fig 1: barplot

我想仅添加 条形顶部每个索引的总量,如下所示:

图 2: 总金额

但是,当我使用以下代码时,我只获得每个堆栈的一部分酒吧。

import matplotlib.pyplot as plt

data = [['0.01 - 0.1','A'],['0.1 - 0.5','B'],['0.5 - 1.0','B'],['0.01 - 0.1','C'],['> 2.5','A'],['1.0 - 2.5','A'],['> 2.5','A']]

df = pd.DataFrame(data, columns = ['Size','Index'])

### plot

df_new = df.sort_values(['Index'])

list_of_colors_element = ['green','blue','yellow','red','purple']

# Draw
piv = df_new.assign(dummy=1) \
            .pivot_table('dummy', 'Index', 'Size', aggfunc='count', fill_value=0) \
            .rename_axis(columns=None)
ax = piv.plot.bar(stacked=True, color=list_of_colors_element, rot=0, width=1)

ax.bar_label(ax.containers[0],fontsize=9)

# Decorations
plt.title("Index coloured by size", fontsize=22)
plt.ylabel('Amount')
plt.xlabel('Index')
plt.grid(color='black', linestyle='--', linewidth=0.4)
plt.xticks(range(3),fontsize=15)
plt.yticks(fontsize=15)

plt.show()

我尝试过不同种类的 ax.bar_label(ax.containers[0],fontsize=9) 但没有一个显示条形总数​​。

I am working with the following bar plot:

Fig 1: bar plot

And I would like to add only the total amount of each index on top of the bars, like this:

Fig 2: with total amount

However, when I use the following code, I only get parts of the stacks of each bar.

import matplotlib.pyplot as plt

data = [['0.01 - 0.1','A'],['0.1 - 0.5','B'],['0.5 - 1.0','B'],['0.01 - 0.1','C'],['> 2.5','A'],['1.0 - 2.5','A'],['> 2.5','A']]

df = pd.DataFrame(data, columns = ['Size','Index'])

### plot

df_new = df.sort_values(['Index'])

list_of_colors_element = ['green','blue','yellow','red','purple']

# Draw
piv = df_new.assign(dummy=1) \
            .pivot_table('dummy', 'Index', 'Size', aggfunc='count', fill_value=0) \
            .rename_axis(columns=None)
ax = piv.plot.bar(stacked=True, color=list_of_colors_element, rot=0, width=1)

ax.bar_label(ax.containers[0],fontsize=9)

# Decorations
plt.title("Index coloured by size", fontsize=22)
plt.ylabel('Amount')
plt.xlabel('Index')
plt.grid(color='black', linestyle='--', linewidth=0.4)
plt.xticks(range(3),fontsize=15)
plt.yticks(fontsize=15)

plt.show()

I have tried with different varieties of ax.bar_label(ax.containers[0],fontsize=9) but none displays the total of the bars.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2025-01-17 22:53:00

正如特伦顿指出的那样, bar_label< /a> 仅当最上面的段永远不为零(即存在于每个堆栈中)时才可用,否则不可用。以下是这两种情况的示例。


如果最上面的段永远不为零,请使用 bar_label

在此示例中,所有 A 都存在最上面的段(紫色 '>2.5') 、 BC,因此我们可以只使用 ax.bar_label(ax.containers[-1])

df = pd.DataFrame({'Index': [*'AAAABBCBC'], 'Size': ['0.01-0.1', '>2.5', '1.0-2.5', '>2.5', '0.1-0.5', '0.5-1.0', '0.01-0.1', '>2.5', '>2.5']})
piv = pd.crosstab(df['Index'], df['Size'])
ax = piv.plot.bar(stacked=True)

# auto label since none of the topmost segments are missing
ax.bar_label(ax.containers[-1])

< /a>


否则,手动求和和标记

在 OP 的示例中,最上面的段(紫色 '>2.5')并不总是存在(BC),因此需要手动求和。

如何计算总数将取决于您的特定数据框。在 OP 的情况下,ABC 是行,因此总计应计算为 sum(axis=1):

df = pd.DataFrame({'Index': [*'AAAABBC'], 'Size': ['0.01-0.1', '>2.5', '1.0-2.5', '>2.5', '0.1-0.5', '0.5-1.0', '0.01-0.1']})
piv = pd.crosstab(df['Index'], df['Size'])
ax = piv.plot.bar(stacked=True)

# manually sum and label since some topmost segments are missing
for x, y in enumerate(piv.sum(axis=1)):
    ax.annotate(y, (x, y+0.1), ha='center')

< /a>

As Trenton points out, bar_label is usable only if the topmost segment is never zero (i.e., exists in every stack) but otherwise not. Here are examples of the two cases.


If the topmost segment is never zero, use bar_label

In this example, the topmost segment (purple '>2.5') exists for all A, B, and C, so we can just use ax.bar_label(ax.containers[-1]):

df = pd.DataFrame({'Index': [*'AAAABBCBC'], 'Size': ['0.01-0.1', '>2.5', '1.0-2.5', '>2.5', '0.1-0.5', '0.5-1.0', '0.01-0.1', '>2.5', '>2.5']})
piv = pd.crosstab(df['Index'], df['Size'])
ax = piv.plot.bar(stacked=True)

# auto label since none of the topmost segments are missing
ax.bar_label(ax.containers[-1])


Otherwise, sum and label manually

In OP's example, the topmost segment (purple '>2.5') does not always exist (missing for B and C), so the totals need to be summed manually.

How to compute the totals will depend on your specific dataframe. In OP's case, A, B, and C are rows, so the totals should be computed as sum(axis=1):

df = pd.DataFrame({'Index': [*'AAAABBC'], 'Size': ['0.01-0.1', '>2.5', '1.0-2.5', '>2.5', '0.1-0.5', '0.5-1.0', '0.01-0.1']})
piv = pd.crosstab(df['Index'], df['Size'])
ax = piv.plot.bar(stacked=True)

# manually sum and label since some topmost segments are missing
for x, y in enumerate(piv.sum(axis=1)):
    ax.annotate(y, (x, y+0.1), ha='center')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文