仅在堆叠条形顶部添加总值
我正在使用以下条形图:
我想仅添加 条形顶部每个索引的总量,如下所示:
但是,当我使用以下代码时,我只获得每个堆栈的一部分酒吧。
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:
And I would like to add only the total amount of each index on top of the bars, like this:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如特伦顿指出的那样,
bar_label
< /a> 仅当最上面的段永远不为零(即存在于每个堆栈中)时才可用,否则不可用。以下是这两种情况的示例。如果最上面的段永远不为零,请使用
bar_label
在此示例中,所有
A
都存在最上面的段(紫色'>2.5'
) 、B
和C
,因此我们可以只使用ax.bar_label(ax.containers[-1])
:否则,手动求和和标记
在 OP 的示例中,最上面的段(紫色
'>2.5'
)并不总是存在(B
和C
),因此需要手动求和。如何计算总数将取决于您的特定数据框。在 OP 的情况下,
A
、B
和C
是行,因此总计应计算为sum(axis=1)
: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 allA
,B
, andC
, so we can just useax.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 forB
andC
), 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
, andC
are rows, so the totals should be computed assum(axis=1)
: