Adding custom labels to axes in a seaborn plot in Python

This tutorial will teach you how to create your own custom labels for the axes of graphs in Python seaborn plot. To do this, we will be creating a graph using seaborn, change its axes’ labels and then use matplotlib to display the plot.

Importing the Libraries

We first import the two libraries using the following piece of code:

import seaborn as sns
import matplotlib.pyplot as plt

pyplot is a simple module based on matplotlib that allows you to plot graphs very easily, similar to what is done in MATLAB (if you are interested).

 

We create alias using the ‘as’ keyword that allows us to write more readable code. I recommend using alias while using libraries as it makes calling functions from these libraries quite simple.

The Dataset

You may use any dataset that you wish to use for this program. However, for the sake of this example, I will be using the ‘titanic’ dataset that stores information about the people that traveled on the Titanic.

Be connected to the internet while running the code because seaborn retrieves this dataset from the internet. This means that you need not have the dataset locally.

The Code and its Explanation

#Importing the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt

#Loading the dataset into the variable 'dataset'
dataset = sns.load_dataset("titanic")

#Graph is created and stored in the variable 'graph'
graph = sns.barplot(x="sex",y="survived",data=dataset)

#The values for labels of x and y axes are taken from the keyboard
x_axis = input("Enter The x-axis label : ")
y_axis = input("Enter The y-axis label : ")

#The custom labels are set to the x and y axes
graph.set(xlabel = x_axis, ylabel=y_axis)

#The plot is shown
plt.show()

We first import the libraries that we need.

Next, we use the sns.load_dataset() function to load the ‘titanic’ dataset into the variable, ‘dataset’.

Subsequently, we use the sns.barplot() function to plot the graph from the dataset between the columns, ‘sex’ and ‘survived’. This denotes the number of males and females that survived in the Titanic tragedy.

In the next part, the input() function takes the custom x and y axes’ label values from the user using the keyboard.

Next, the set() function sets the x and y axes labels to the ones you entered in the previous step.

Finally, the plt.show() function shows the graph.

I have set the x-axis label and y-axis label to ‘Example x_axis’ and ‘Example y_axis’ respectively for the sake of this example.

Below is the result we can see after we run our program:

custom labels to axes in seaborn plot

 

In conclusion, I recommend you to explore more about Seaborn and graphs in Python as the combination of both these modules along with another library called ‘pandas’ is some of the most used libraries in Python. You can go to the following links to learn more:

Plotting Categorical Data with Seaborn in Python

Seaborn module of Python

Python Matplotlib Library

Plotting a Histogram in Python using Seaborn

Leave a Reply

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