Seaborn | Categorical Plots
Last Updated :
08 Oct, 2021
Plots are basically used for visualizing the relationship between variables. Those variables can be either be completely numerical or a category like a group, class or division. This article deals with categorical variables and how they can be visualized using the Seaborn library provided by Python.
Seaborn besides being a statistical plotting library also provides some default datasets. We will be using one such default dataset called ‘tips’. The ‘tips’ dataset contains information about people who probably had food at a restaurant and whether or not they left a tip for the waiters, their gender, whether they smoke and so on.
Let us have a look at the tips dataset.
Code
Python3
import seaborn as sns
from warnings import filterwarnings
df = sns.load_dataset('tips')
df.head()
|
Now lets proceed onto the plots so that we can how we can visualize these categorical variables.
Barplot
A barplot is basically used to aggregate the categorical data according to some methods and by default its the mean. It can also be understood as a visualization of the group by action. To use this plot we choose a categorical column for the x axis and a numerical column for the y axis and we see that it creates a plot taking a mean per categorical column.
Syntax:
barplot([x, y, hue, data, order, hue_order, …])
Example:
Python3
sns.set_style('darkgrid')
sns.barplot(x ='sex', y ='total_bill', data = df, palette ='plasma')
import numpy as np
sns.barplot(x ='sex', y ='total_bill', data = df,
palette ='plasma', estimator = np.std)
|
Output:

Explanation/Analysis
Looking at the plot we can say that the average total_bill for the male is more as compared to the female.
- palette is used to set the color of the plot
- estimator is used as a statistical function for estimation within each categorical bin.
Countplot
A countplot basically counts the categories and returns a count of their occurrences. It is one of the most simple plots provided by the seaborn library.
Syntax:
countplot([x, y, hue, data, order, …])
Example:
Python3
sns.countplot(x ='sex', data = df)
|
Output:

Explanation/Analysis
Looking at the plot we can say that the number of males is more than the number of females in the dataset. As it only returns the count based off a categorical column, we need to specify only the x parameter.
Boxplot
A boxplot is sometimes known as the box and whisker plot.It shows the distribution of the quantitative data that represents the comparisons between variables. boxplot shows the quartiles of the dataset while the whiskers extend to show the rest of the distribution i.e. the dots indicating the presence of outliers.
Syntax:
boxplot([x, y, hue, data, order, hue_order, …])
Example:
Python3
sns.boxplot(x ='day', y ='total_bill', data = df, hue ='smoker')
|
Output:

Explanation/Analysis –
x takes the categorical column and y is a numerical column.Hence we can see the total bill spent each day.”hue” parameter is used to further add a categorical separation. By looking at the plot we can say that the people who do not smoke had a higher bill on Friday as compared to the people who smoked.
Violinplot
It is similar to the boxplot except that it provides a higher, more advanced visualization and uses the kernel density estimation to give a better description about the data distribution.
Syntax:
violinplot([x, y, hue, data, order, …])
Example:
Python3
sns.violinplot(x ='day', y ='total_bill', data = df, hue ='sex', split = True)
|
Output:

Explanation/Analysis –
- hue is used to separate the data further using the sex category
- setting split=True will draw half of a violin for each level. This can make it easier to directly compare the distributions.
Stripplot
It basically creates a scatter plot based on the category.
Syntax:
stripplot([x, y, hue, data, order, …])
Example:
Python3
sns.stripplot(x ='day', y ='total_bill', data = df,
jitter = True, hue ='smoker', dodge = True)
|
Output:

Explanation/Analysis –
- One problem with strip plot is that you can’t really tell which points are stacked on top of each other and hence we use the jitter parameter to add some random noise.
- jitter parameter is used to add an amount of jitter (only along the categorical axis) which can be useful when you have many points and they overlap, so that it is easier to see the distribution.
- hue is used to provide an addition categorical separation
- setting split=True is used to draw separate strip plots based on the category specified by the hue parameter.
Swarmplot
It is very similar to the stripplot except the fact that the points are adjusted so that they do not overlap.Some people also like combining the idea of a violin plot and a stripplot to form this plot. One drawback to using swarmplot is that sometimes they dont scale well to really large numbers and takes a lot of computation to arrange them. So in case we want to visualize a swarmplot properly we can plot it on top of a violinplot.
Syntax:
swarmplot([x, y, hue, data, order, …])
Example:
Python3
sns.swarmplot(x ='day', y ='total_bill', data = df)
|
Output:

Example:
Python3
sns.violinplot(x ='day', y ='total_bill', data = df)
sns.swarmplot(x ='day', y ='total_bill', data = df, color ='black')
|
Output:

Factorplot
It is the most general of all these plots and provides a parameter called kind to choose the kind of plot we want thus saving us from the trouble of writing these plots separately. The kind parameter can be bar, violin, swarm etc.
Syntax:
sns.factorplot([x, y, hue, data, row, col, …])
Example:
Python3
sns.factorplot(x ='day', y ='total_bill', data = df, kind ='bar')
|
Output:

Get IBM Certification and a 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
Master Machine Learning, Data Science & AI with this complete program and also get a 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
Seaborn | Categorical Plots
Plots are basically used for visualizing the relationship between variables. Those variables can be either be completely numerical or a category like a group, class or division. This article deals with categorical variables and how they can be visualized using the Seaborn library provided by Python.
5 min read
ML | Matrix plots in Seaborn
Seaborn is a wonderful visualization library provided by python. It has several kinds of plots through which it provides the amazing visualization capabilities. Some of them include count plot, scatter plot, pair plots, regression plots, matrix plots and much more. This article deals with the matrix
4 min read
Seaborn | Distribution Plots
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. This article deals with the distribution plots in seaborn which is used for examining univariate and bivariate distributions. In this ar
3 min read
Multi-plot grid in Seaborn
Prerequisites: Matplotlib, Seaborn In this article, we are going to see multi-dimensional plot data, It is a useful approach to draw multiple instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of information about a complex dataset.
6 min read
Seaborn - Color Palette
In this article, We are going to see seaborn color_palette(), which can be used for coloring the plot. Using the palette we can generate the point with different colors . In this below example we can see the palette can be responsible for generating the different colormap values. Syntax: seaborn.col
4 min read
Python Seaborn - Catplot
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major problems faced by Matplotlib; the problems are? Default Matplotlib parametersWorking with data frame
5 min read
Scatter Plot Matrix
In a dataset, for k set of variables/columns (X1, X2, ....Xk), the scatter plot matrix plot all the pairwise scatter between different variables in the form of a matrix. Scatter plot matrix answer the following questions: Are there any pair-wise relationships between different variables? And if ther
3 min read
seaborn.crayon_palette Method()
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. There is just something extraordinary about a well-designed visualization. In this article we will learn about the Python seaborn.crayon
2 min read
Seaborn | Regression Plots
The regression plots in seaborn are primarily intended to add a visual guide that helps to emphasize patterns in a dataset during exploratory data analyses. Regression plots as the name suggests creates a regression line between 2 parameters and helps to visualize their linear relationships. This ar
4 min read
Handling Categorical Data in Python
Categorical data is a set of predefined categories or groups an observation can fall into. Categorical data can be found everywhere. For instance, survey responses like marital status, profession, educational qualifications, etc. However, certain problems can arise with categorical data that must be
6 min read
Grid Plot in Python using Seaborn
Grids are general types of plots that allow you to map plot types to grid rows and columns, which helps you to create similar character-separated plots. In this article, we will be using two different data sets (Iris and Tips) for demonstrating grid plots Using Iris Dataset We are going to use the I
4 min read
Seaborn Kdeplot – A Comprehensive Guide
Kernel Density Estimate (KDE) Plot is a powerful tool for estimating the probability density function of continuous or non-parametric data. KDE plot is implemented through the kdeplot function in Seaborn. This article explores the syntax and usage of kdeplot in Python, focusing on one-dimensional an
4 min read
Relational plots in Seaborn - Part I
Relational plots are used for visualizing the statistical relationship between the data points. Visualization is necessary because it allows the human to see trends and patterns in the data. The process of understanding how the variables in the dataset relate each other and their relationships are t
4 min read
Plotting graph using Seaborn | Python
This article will introduce you to graphing in Python with Seaborn, which is the most popular statistical visualization library in Python. Installation: The easiest way to install seaborn is to use pip. Type following command in terminal: pip install seaborn OR, you can download it from here and ins
8 min read
Relational plots in Seaborn - Part II
Prerequisite: Relational Plots in Seaborn - Part IIn the previous part of this article, we learnt about the relplot(). Now, we will be reading about the other two relational plots, namely scatterplot() and lineplot() provided in seaborn library. Both these plots can also be drawn with the help of ki
6 min read
Seaborn | Style And Color
Seaborn is a statistical plotting library in python. It has beautiful default styles. This article deals with the ways of styling the different kinds of plots in seaborn. Seaborn Figure Styles This affects things like the color of the axes, whether a grid is enabled by default, and other aesthetic e
4 min read
Handling Categorical Data with Bokeh - Python
As a data scientist, you will often come across datasets with categorical data. Categorical data is a type of data that can be divided into distinct categories or groups. For example, a dataset might have a column with the categories "red", "green", and "blue". Handling categorical data can be chall
5 min read
Plotting graph For IRIS Dataset Using Seaborn And Matplotlib
Matplotlib.pyplot library is most commonly used in Python in the field of machine learning. It helps in plotting the graph of large dataset. Not only this also helps in classifying different dataset. It can plot graph both in 2d and 3d format. It has a feature of legend, label, grid, graph shape, gr
2 min read
Visualising ML DataSet Through Seaborn Plots and Matplotlib
Working on data can sometimes be a bit boring. Transforming a raw data into an understandable format is one of the most essential part of the whole process, then why to just stick around on numbers, when we can visualize our data into mind-blowing graphs which are up for grabs in python. This articl
7 min read