Plotting Categorical Data with Seaborn in Python
This tutorial will teach you how to plot categorical data in Python using the Seaborn library.
Before we begin, you must know what the seaborn library is all about. You must also have the Seaborn library installed on your system.
Seaborn is a data visualization library based on matplotlib and you can use it to create beautiful, detailed graphs using Python.
If you have seaborn already installed on your system, you can skip this step.
To install Seaborn, you can refer to the following page:-
Importing the required libraries
First of all, we will import the required libraries before using them.
import seaborn as sns
The ‘as’ keyword creates an alias. So, whenever we want to use a function from seaborn, we can just use ‘sns’ instead.
What Is Categorical Data?
Categorical Data is nothing but data that has categories, as the name implies. For example, we can consider gender to be categorical data (male and female). Similarly, hair color, skin tone, etc are all examples of categorical data.
An important aspect of categorical data is that categorical data can be sorted in any way; the meaning of the data will remain the same. This means that there is no implicit ordering to categorical data and can be ordered in any random way.
Categorical Data can be plotted in many ways. In this tutorial, I will be showing you how to plot categorical data using bar plots as it is very commonly used.
Let’s get started!
We can plot categorical graphs in various different ways. In this tutorial, I will be using a dataset that represents the information about the people that were on the Titanic when it tragically sunk.
I will recommend you to download this dataset from the link below:-
Python code: Plot Categorical Data with Seaborn in Python
#importing the libraries required import seaborn as sns #Loading the dataset dataset = sns.load_dataset('titanic') #Plottint the bar plot sns.barplot(x="sex",y="survived",data=dataset)
This code will create a bar plot that shows the number of females and males that survived in the Titanic tragedy.
The ‘sns.load_dataset’ function loads a dataset into a variable. The ‘sns.barplot()’ function plots the graph and shows the output.
The x value i.e., ‘sex’, in this case, is the set of values of the x-axis that are taken from the dataset. The y value i.e., ‘survived’, is the set of values of y-axis taken from the dataset.