# Create a Contour Plot in Seaborn

Salman Mehmood May 11, 2022

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:

Output:

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

seaborn.kdeplot(data_set.horsepower, data_set.mpg)
plot.show()

Output:

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

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

Output:

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

import seaborn as seaborn
import matplotlib.pyplot as plot

plot.show()

Output:

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

import seaborn as seaborn
import matplotlib.pyplot as plot