In this article, we will go through seaborn countplot using sns.countplot() function for visualizing data of your machine learning or data science project. The countplot is majorly used for showing the observational count in different category based bins with the help of bars.
The main idea of the count plot is similar to barplot() function. It will appear to do similar tasks but there are some differences that we’ll learn through different examples in this tutorial.
Seaborn Countplot Tutorial
Seaborn library has a function countpot() for creating couplot, let us take a look at its syntax and parameters.
seaborn.countplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, kwargs)**
- x, y, hue : names of variables in data or vector data, optional
This is the input provided for building the plot.
- data : DataFrame, array, or list of arrays, optional
Here we pass the data for the purpose of plotting the graph.
- order, hue_order : lists of strings, optional
This is the order used for plotting categorical levels.
- orient : “v” | “h”, optional
Through this parameter, we can set the orientation of plot as horizontal or vertical.
- color : matplotlib color, optional
In this parameter, we are setting the color of the plot.
- palette : palette name, list, or dict
The palette will be deciding the colors for the graph.
- ax : matplotlib Axes, optional
These are the axes over which the plot is built.
1st Example – Seaborn Countpot with Single Categorical variable
The first example shows how we can build a simple countplot in seaborn with data having a single categorical variable. As always, necessary libraries are imported, the theme is set, and the in-built dataset is loaded.
We then use the countplot() function for plotting the count plot.
import seaborn as sns sns.set_theme(style="darkgrid") titanic = sns.load_dataset("titanic") ax = sns.countplot(x="class", data=titanic)
2nd Example – Seaborn Countplot with Two Variables using Hue
This 2nd example will showcase the way in which we can plot two categorical variables in the countplot graph in Seaborn.
The function countplot is called with x and hue parameter. We pass the “class” variable as x and the second variable “who” as hue. This results in the categorization of class variable x based on hue variable “who”.
In addition to all of this, we are assigning the colors of the plot with the help of palette parameter.
ax = sns.countplot(x="class", hue="who", data=titanic, palette="spring")
3rd Example – Horizontal Countplot in Seaborn
The previous examples had built vertical count plots, in this third example, we will build a horizontal count plot.
To change the orientation, we will replace x with y. In this way, we will obtain a horizontal count plot.
ax = sns.countplot(y="class", hue="who", data=titanic, palette="rainbow")
4th Example – Applying styling to the Countplot bar
This example of this tutorial shows how we can style the countplot bar. For this purpose, we use the facecolor, linewidth, and edgecolor parameters in the countplot() function.
ax = sns.countplot(x="who", data=titanic, facecolor=(0, 0, 0, 0), linewidth=5, edgecolor=sns.color_palette("dark", 3))
This is the end of this tutorial on countplot using the seaborn library. We looked at the syntax of countplot) and saw multiple examples with different usages of the parameters.