Annotate Bars in Barplot with Pandas and Matplotlib

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Annotate Bars in a single-group Bar chart with Pandas and Matplotlib
  2. Annotate Bars in a multi-group Bar chart with Pandas and Matplotlib
  3. Annotate Bars in a multi-group Bar chart with Pandas using Axes.patches

# Annotate Bars in a single-group Bar chart with Pandas and Matplotlib

To annotate bars in a single-group bar chart with Pandas and Matplotlib:

  1. Call the Axes.bar_label() method to add labels to the bars in the bar container.
  2. Pass the first BarContainer object as an argument to the bar_label() method.
main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar(x='x', y='y') ax.bar_label(ax.containers[0]) plt.show()

annotate bars in barplot with pandas and matplotlib

The code for this article is available on GitHub

The Axes.bar_label() method adds labels to the bars in the supplied BarContainer.

The Axes.bar_label() method basically enables us to auto-label bar charts.

The only argument we passed to the method is the first artist in the BarContainer object.

This approach also works if you have a horizontal bar plot.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.barh(x='x', y='y') ax.bar_label(ax.containers[0]) plt.show()

works with horizontal bar plot

The code for this article is available on GitHub

# Annotate Bars in a multi-group Bar chart with Pandas and Matplotlib

However, if you need to annotate bars in a multi-group bar chart, you have to use a for loop to iterate over the BarContainer objects and pass each container to axes.bar_label().

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar() for container in ax.containers: ax.bar_label(container) plt.show()

annotate bars in multi group bar chart with pandas

The code for this article is available on GitHub

The ax.containers attribute returns a list containing the artists for the x and y axes.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar() for container in ax.containers: ax.bar_label(container) # [<BarContainer object of 5 artists>, <BarContainer object of 5 artists>] print(ax.containers)

On each iteration of the for loop, we pass the current BarContainer to the Axes.bar_label() method to label the bar plot.

You can also use the padding argument if you need to increase the distance to the label from the end to the bar.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar() for container in ax.containers: ax.bar_label(container, padding=2, fontsize=8) plt.show()

set padding argument when calling bar label

The code for this article is available on GitHub

I also set the fontsize argument but you don't have to.

This approach also works for a multi-group horizontal bar plot.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.barh() for container in ax.containers: ax.bar_label(container) plt.show()

works for multi group horizontal bar plot

# Annotate Bars in a multi-group Bar chart with Pandas using Axes.patches

You can also use the patches property to annotate bars in a multi-group bar chart.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar() for patch in ax.patches: ax.annotate( str(patch.get_height()), (patch.get_x() * 1.000, patch.get_height() * 1.008), fontsize=8, ) plt.show()

annotate bars in multi group bar chart using patches

The code for this article is available on GitHub

Patches are Artists with a face color and an edge color.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'x': [0.79, 0.62, 0.65, 0.22, 0.47], 'y': [0.97, 0.26, 0.88, 0.63, 0.63], }, index=['A', 'B', 'C', 'D', 'E']) ax = df.plot.bar() # <Axes.ArtistList of 10 patches> print(ax.patches) for patch in ax.patches: ax.annotate( str(patch.get_height()), (patch.get_x() * 1.000, patch.get_height() * 1.008), fontsize=8, ) plt.show()
The code for this article is available on GitHub

On each iteration of the for loop, we use the Axes.annotate() method to annotate the current bar.

You might have to adjust the multipliers in the second argument and the fontsize depending on your needs.

The patch.get_height() method returns the height of the rectangle.

The patch.get_x() method returns the left coordinate of the rectangle.

I've also written a guide on how to annotate data points while plotting from a DataFrame in Pandas.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.