• 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 Barplot with bar_label() in Matplotlib

datavizpyr · May 20, 2021 ·

Annotating barplots with labels like texts or numerical values can be helpful to make the plot look better. Till now, one of the options add annotations in Matplotlib is to use pyplot’s annotate() function. Starting from Matplotlib version 3.4.2 and above, we have a new function, axes.bar_label() that lets you annotate barplots with labels easily.

In this tutorial, we will learn how to add labels to barplots using bar_label() function. As before, we will make barplots using Seaborn’s barplot() function, but add labels using Matplotlib’s bar_label() function.

Let us load Pandas, Seaborn and Matplotlib.

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

Let us create some data to make the barplot.

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

Our data for making barplot looks like this.

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

Let us make simple barplot with Seaborn’s barplot() function. Here we have customized the barplot’s labels with Matplotlib’s function xlabel and ylabel.

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

Barplot with Seaborn
Barplot with Seaborn

Labeling Barplots with Matplotlib’s bar_label() function

Similar to customizing the plot labels, now we can customize the bar labels using bar_label() function. Now, let us specify the bar labels using bar_label() function after making the barplot. Here we add bar height as bar labels to make it easy to read the barplot.

plt.figure(figsize=(8, 6))
splot=sns.barplot(x="continent",y="lifeExp",data=df)
plt.xlabel("Continent", size=16)
plt.ylabel("LifeExp", size=16)
plt.bar_label(splot.containers[0])
plt.savefig("annotate_barplot_with_Matplotlib_bar_label_Python.png")

Barplot function returns a bar container with all “artists” describing the barplot. Matplotlib’s bar_label() function takes a container as input to add labels/annotations to the barplot.

Adding labels to barplot with bar_label() in Matplotlib
annotate_barplot_with_Matplotlib_bar_label_Python

Customizing Barplot Labels with Matplotlib’s bar_label()

We can customize the label size using size argument to bar_label() function.

plt.figure(figsize=(8, 6))
splot=sns.barplot(x="continent",y="lifeExp",data=df)
plt.xlabel("Continent", size=16)
plt.ylabel("LifeExp", size=16)
plt.bar_label(splot.containers[0],size=16)
plt.savefig("annotate_barplot_with_Matplotlib_bar_label_change_size_Python.png")
Customize Label Size Matplotlib's bar_label()
Customize Label Size Matplotlib’s bar_label()

We can also place the bar label in the middle of the bars instead of at the bar edges using “label_type” argument.

plt.figure(figsize=(8, 6))
splot=sns.barplot(x="continent",y="lifeExp",data=df)
plt.xlabel("Continent", size=16)
plt.ylabel("LifeExp", size=16)
plt.bar_label(splot.containers[0],size=16,label_type='center')
plt.savefig("annotate_barplot_with_Matplotlib_bar_label_at_center_Python.png")
Place bar label to center with Matplotlib's bar_label()
Place bar label to center with Matplotlib’s bar_label()

Related posts:

Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis Grouped Barplot with SeabornHow To Make Grouped Barplots in Python with Seaborn? Seaborn Scatterplot: Change edgecolor and line widthHow To Change Edge Color on Seaborn Scatter Plot? Grouped Boxplot with Jittered Data Points SeabornGrouped Boxplot with Jittered Points with Seaborn Python

Filed Under: Matplotlib, Matplotlib bar_label, Python, Seaborn Tagged With: Matplotlib bar_label, Python

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