pythonmatplotlibbar-chartplot-annotations

bar labels with new f-string format style


As of matplotlib 3.4.0, Axes.bar_label method allows for labelling bar charts.

However, the labelling format option works with old style formatting, e.g. fmt='%g'

How can I make it work with new style formatting that would allow me to do things like percentages, thousands separators, etc: '{:,.2f}', '{:.2%}', ...

The first thing that comes to my mind is somehow taking the initial labels from ax.containers and then reformatting them but it also needs to work for different bar structures, grouped bars with different formats and so on.


Solution

  • How can I make bar_label work with new style formatting like percentages, thousands separators, etc?


    Examples: