Skip to content
Advertisement

Add ONLY the total values on top of stacked bars in matplotlib

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.

JavaScript

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

Any help is appreciated!

Advertisement

Answer

I’m not sure there’s a way to do this with the new bar_label method, but you can label the totals manually.

Compute the row totals via piv.sum(axis=1) and annotate the totals:

JavaScript

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement