Add Values on Seaborn Barplot in Python

In this tutorial, I’ll guide you through creating bar plots using Seaborn and further customizing by adding labels and showing data values on bars so you can look at them directly in Plot in Python. Gaining insight from your datasets will be easier if you know how to use Seaborn for bar plots, regardless of your experience as a data scientist.

Prerequisites

Before we begin, make sure you have the necessary libraries installed:

pip install seaborn matplotlib

If not installed, copy the above line in your terminal or command prompt and hit Enter. You can replace pip with pip3 according to your system version.

Step 1: Importing Libraries

You can start by importing all the necessary libraries.

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random

Step 2: Loading Dataset

Load the dataset and extract the columns you want to generate the barplot. I am creating two arrays of 20 unique random integers for demonstration purposes using Numpy and random library.

X = np.random.choice(range(1,101),size=20, replace=False)
Y = np.random.choice(range(1,101),size=20, replace=False)

print(X)
print(Y)

It will generate the following output:

[20 80 26 84 9 27 53 47 83 8 62 7 51 67 93 96 90 91 77 33] 
[11 89 50 7 13 44 42 56 87 78 66 92 37 94 49 61 32 27 12 19]

Step 3: Creating Seaborn BarPlot

Let’s create the simple Seaborn barplot to visualize the above array.

sns.barplot(x=X, y= Y)
plt.show()

It will generate the following barplot:

Creating Seaborn BarPlot

Step 4: Customising the plot

Enhance the visual appeal of the plot by customizing it as per your requirements.

fig, ax = plt.subplots(figsize=(12,8))

sns.barplot(
    x = X,
    y = Y,
    ax = ax
)

ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.bar_label(ax.containers[0])

plt.show()

The ax.bar_label function in Matplotlib adds labels to bars in a bar plot. Here are some of the key parameters:

  • ax.containers[0] gives you access to the first container, which contains the bars of the first group.
  • fmt: An optional parameter that allows you to specify a format string for the labels. This is useful for formatting the appearance of the labels, such as specifying the number of decimal places or adding percentages.
  • label_type: An optional parameter that determines the location of the labels. Possible values are edge (default) and center.
    edge places labels at the top of the bars, and center place labels at the center.

The output of the above code is:

output - add value to seaborn barplot

Conclusion

Congratulations! A Seaborn bar plot, along with labels and values, has been successfully developed by you. The fundamentals of loading data, importing libraries, making a basic plot, and adjusting the look were all addressed in this tutorial.

Please feel free to experiment with Seaborn’s other capabilities and modify these methods to fit your datasets and visualization requirements.

Happy plotting!

Leave a Reply

Your email address will not be published. Required fields are marked *