• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Annotate Bars in Barplot with Matplotlib in Python?

datavizpyr · May 29, 2020 ·

In this post we will learn examples of adding text, annotating bars in barplot using matplotlib. We will make bar plots using Seaborn’s barplot and use Matplotlib to add annotations to the bars in barplot.

Let us load Pandas, Seaborn and Matplotlib.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

Let us create a toy dataset using two lists.

continent = ["Africa","Americas","Asia","Europe","Oceania"]
lifeExp=[49,65,60,72,74]

And create a Pandas dataframe from the two lists.

df = pd.DataFrame({"continent":continent, "lifeExp":lifeExp})
df

      continent	lifeExp
0	Africa	49
1	Americas	65
2	Asia	60
3	Europe	72
4	Oceania	74

We have the data ready to make a barplot and annotate the bars. Let us start with making a simple barplot using Seaborn’s barplot() function.

plt.figure(figsize=(8, 6))
sns.barplot(x="continent",y="lifeExp",data=df)
plt.xlabel("Continent", size=14)
plt.ylabel("LifeExp", size=14)
plt.savefig("bar_plot_Seaborn_Python.png")

The barplot shows average life expectancy values as bar for each continent from gapminder dataset.

Simple Barplot with Seaborn
Simple Barplot with Seaborn

Adding Annotation to Barplots with annotate() function in Matplotlib

Sometimes, it may be useful to add the actual values of bar height on each bar in a barplot. To annotate bars in barplot made with Seaborn, we will use Matplotlib’s annotate function. Matplotlib’s annotate() function is pretty versatile and we can customize various aspects of annotation in a plot.

In the code below, we loop through each bar in the Seaborn barplot object and use annotate() function to get the height of the bar, decide the location to annotate using barwidth, height and its coordinates. We can also control the size the text on top of each bar.

plt.figure(figsize=(8, 6))
splot=sns.barplot(x="continent",y="lifeExp",data=df)
for p in splot.patches:
    splot.annotate(format(p.get_height(), '.1f'), 
                   (p.get_x() + p.get_width() / 2., p.get_height()), 
                   ha = 'center', va = 'center', 
                   xytext = (0, 9), 
                   textcoords = 'offset points')
plt.xlabel("Continent", size=14)
plt.ylabel("LifeExp", size=14)
plt.savefig("add_text_to_top_of_bars_in_barplot_Seaborn_Python.png")

Now we get barplot with annotations on each bar showing the heights of the bar. With the argument xytext, we have annotation on top of the bars.

How To Label Bars in Barplot with Python?
How To Add Labels on top of Bars in Barplot with Python?

We can customize annotation a bit. Sometimes adding annotation over the bar can overlap with the plot outline. A solution is to add the annotation inside the bars of barplot. Here we changed the position using xytext.

plt.figure(figsize=(8, 6))
splot=sns.barplot(x="continent",y="lifeExp",data=df)
for p in splot.patches:
    splot.annotate(format(p.get_height(), '.1f'), 
                   (p.get_x() + p.get_width() / 2., p.get_height()), 
                   ha = 'center', va = 'center', 
                   size=15,
                   xytext = (0, -12), 
                   textcoords = 'offset points')
plt.xlabel("Continent", size=14)
plt.ylabel("LifeExp", size=14)
plt.savefig("add_annotation_to_bars_in_barplot_Seaborn_Python.png")

We get a barplot with annotation inside the bars. In the example here we have also increased the font size of the annotation with size argument.

How To Annotate Bars in Barplot with Matplotlib?
How To Annotate Bars in Seaborn Barplot with Matplotlib?

Annotating barplots with Matplotlib’s bar_label()

Starting from Matplotlib version 3.4.2 and above, Matplotlib has added a new function, bar_label(), to annotate barplots easily. Check out the following post to learn how to use Matplotlib’s bar_label() function to add annotations.

Related posts:

Customize Mean Mark Boxplot: Seaborn & MatplotlibHow to Show Mean on Boxplot using Seaborn in Python? How To Move Legend to Outside Plot Seaborn Scatterplot?How To Place Legend Outside the Plot with Seaborn in Python? Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis Grouped Boxplot in Python with SeabornGrouped Boxplots in Python with Seaborn

Filed Under: annotate bars in barplot, Python Tagged With: Matplotlib, Python, Seaborn

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version