Tutorial

Seaborn Kdeplot - A Comprehensive Guide

Published on August 3, 2022
author

Safa Mulani

Seaborn Kdeplot - A Comprehensive Guide

Hey, folks! In our Seaborn tutorial, we will be focusing on Seaborn Kdeplot.


What is Kdeplot?

Kdeplot is a Kernel Distribution Estimation Plot which depicts the probability density function of the continuous or non-parametric data variables i.e. we can plot for the univariate or multiple variables altogether. Using the Python Seaborn module, we can build the Kdeplot with various functionality added to it.

In order to use the Seaborn module, we need to install and import the module using the below command:

pip install seaborn

import seaborn

Creating a Univariate Seaborn Kdeplot

The seaborn.kdeplot() function is used to plot the data against a single/univariate variable. It represents the probability distribution of the data values as the area under the plotted curve.

Syntax:

seaborn.kdeplot(data)

Example 1:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data)
plt.show()

In the above example, we have generated some random data values using the numpy.random.randn() function.

Output:

Univariate Seaborn Kdeplot
Univariate Seaborn Kdeplot

Example 2:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data,color='green',shade=True)
plt.show()

In the above example, we have highlighted the plot using the parameter - ‘shade’ to highlight the area under the curve. Further, we can set different colors to the plot using the parameter - ‘color’.

Output:

Univariate Seaborn Kdeplot With Shade And Color Parameter
Univariate Seaborn Kdeplot With Shade And Color Parameter

Creating a Bivariate Seaborn Kdeplot

Seaborn Kdeplots can even be used to plot the data against multiple data variables or bivariate(2) variables to depict the probability distribution of one with respect to the other values.

Syntax:

seaborn.kdeplot(x,y)

Thus, the distribution is represented as a contour plot depicting the relationship of the distribution between the two data variables.

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],color='blue',shade=True)
plt.show()

Output:

Bivariate Seaborn Kdeplot
Bivariate Seaborn Kdeplot

Plotting Seaborn Kdeplot along the Vertical axis

We can plot the Kdeplots along the y-axis using the below syntax:

Syntax:

seaborn.kdeplot(data,vertical=True)

Thus, by setting the ‘vertical’ parameter to True, we can plot the distribution against the y-axis.

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],vertical=True,color='blue',shade=True)
plt.show()

Output:

Univariate Seaborn Kdeplot Along Vertical Axis
Univariate Seaborn Kdeplot Along Vertical Axis

Using color palettes within a Seaborn Kdeplot

Different color palettes can be used along with the Seaborn plots to visualize the data in a better manner using the ‘cmap’ parameter.

Different types of color palettes are available at Matplotlib Colormap.

Syntax:

seaborn.kdeplot(data,cmap)

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],shade=True,cmap="Purples_d")
plt.show()

Output:

Bivariate Seaborn Kdeplot With Color Palette
Bivariate Seaborn Kdeplot With Color Palette

Plotting two shaded Bivariate Kdeplots

The two shaded Bivariate Kdeplots help in understanding the variation of the data in terms of the probability distribution of the bivariate group of data variables.

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d")
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Blues")
plt.show()

Output:

Shaded Bivariate Seaborn Kdeplots
Shaded Bivariate Seaborn Kdeplots

Addition of a Colorbar to a Seaborn Kdeplot

A colorbar maps the pictorial representation of values against the original data values and helps visualize the data in a better manner.

Syntax:

seaborn.kdeplot(data,cbar=True)

Example:

import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res=sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d",cbar=True)
plt.show()

Output:

Bivariate Seaborn Kdeplot With Cbar
Bivariate Seaborn Kdeplot With Cbar

Conclusion

Seaborn module is purely built upon the Matplotlib module and the combination is extensively used to visualize the data in different forms.

I would strongly recommend the readers to go through Python Matplotlib Tutorial for a better understanding about the basics of data visualization.


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more