Create a Contour Plot in Seaborn

Salman Mehmood May 11, 2022
Create a Contour Plot in Seaborn

This discussion will introduce how to create a contour plot using the kdeplot() function in Seaborn.

Create a Contour Plot Using the kdeplot() Function in Seaborn

Kernel density estimation allows us to estimate the probability density function from our finite data set. The kdeplot() has the option of the bivariate plot; in this case, we can estimate the joint probability density function for data in two dimensions.

Seaborn’s kdeplot() allows for creating contours representing the different density levels of your data so that you can estimate the joint PDF. Seaborn does not have a contour function, so we need to use the kdeplot() function to display the contour plot.

Let’s look at a Seaborn code to build a bivariate or two-dimensional plot using kdeplot(). We will start by importing pyplot and Seaborn and aliasing both libraries.

import seaborn as seaborn
import matplotlib.pyplot as plot

The next step is to load some data from Seaborn. We will be using a data set about cars, so we have different statistics about various cars.

The dropna() function will drop all null values from the data set.

Code:

data_set=seaborn.load_dataset('mpg').dropna()
data_set.head()

Output:

Seaborn Contour Plot - Output 1

Now, we will use the kdeplot() function and pass the horsepower and the mpg or miles per gallon.

Code:

import seaborn as seaborn
import matplotlib.pyplot as plot

data_set=seaborn.load_dataset('mpg').dropna()
seaborn.kdeplot(data_set.horsepower, data_set.mpg)
plot.show()

Output:

Seaborn Contour Plot - Output 2

There are a couple of various options we can take advantage of here. The first one is having more rings or more different levels in this plot.

We can change that value by accessing the n_levels parameter.

import seaborn as seaborn
import matplotlib.pyplot as plot

data_set=seaborn.load_dataset('mpg').dropna()

seaborn.kdeplot(data_set.horsepower, data_set.mpg,n_levels=20)
plot.show()

Output:

Seaborn Contour Plot - Output 3

We can also switch it over to a shaded version using the shade version.

import seaborn as seaborn
import matplotlib.pyplot as plot

data_set=seaborn.load_dataset('mpg').dropna()
seaborn.kdeplot(data_set.horsepower, data_set.mpg,shade=True)
plot.show()

Output:

Seaborn Contour Plot - Output 4

We can also include a color bar using the cbar parameter.

import seaborn as seaborn
import matplotlib.pyplot as plot

data_set=seaborn.load_dataset('mpg').dropna()
seaborn.kdeplot(data_set.horsepower, data_set.mpg,shade=True,cbar=True)
plot.show()

As you notice, you have two different categories in your data.

Output:

Seaborn Contour Plot - Output 5

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Seaborn Plot