Seaborn Kdeplot – A Comprehensive Guide

25 Nov, 2020
Kernel Density Estimate (KDE) Plot and Kdeplot allows us to estimate the probability density function of the continuous or non-parametric from our data set curve in one or more dimensions it means we can create plot a single graph for multiple samples which helps in more efficient data visualization.
In order to use the Seaborn module, we need to install the module using the below command:
pip install seaborn

Syntax: seaborn.kdeplot(x=None, *, y=None, vertical=False, palette=None, **kwargs)
Parameters:
x, y : vectors or keys in data
vertical : boolean (True or False)
data : pandas.DataFrame, numpy.ndarray, mapping, or sequence
We learn the usage of some parameters through some specific examples:
First import the corresponding library
Python3
import pandas as pdimport seaborn as sbimport numpy as npfrom matplotlib import pyplot as plt%matplotlib inline
Let’s see the Kde of our variable x-axis and y-axis, so let pass the x variable into the kdeplot() methods.
Python3
# data x and y axis for seabornx= np.random.randn(200)y = np.random.randn(200)  # Kde for x varsns.kdeplot(x)
Output:
Then after check for y-axis.
Python3
sns.kdeplot(y)
Output:
We can highlight the plot using shade to the area covered by the curve. If True, shadow processing is performed in the area below the kde curve, and color controls the color of the curve and shadow
Python3
sns.kdeplot(x, shade = True)
Output:
You can change the Shade color with color attributes:
Python3
sns.kdeplot(x, shade = True , color = "Green")
Output:
Python3
sns.kdeplot(x, vertical = True)
Output:
Simple pass the two variables into the seaborn.kdeplot() methods.
Python3
sns.kdeplot(x,y)
Output:
Shade the area covered by a curve with shade attributes:
Python3
sns.kdeplot(x,y, shade = True)
Output:
Now you can change the color with cmap attributes:
Python3
sns.kdeplot(x,y, cmap = "winter_r")
Output:
Use of Cbar: If True, add a colorbar to annotate the color mapping in a bivariate plot. Note: Does not currently support plots with a hue variable well.
Python3
sns.kdeplot(x, y, shade=True, cbar=True)
Output:
Let see the example with Iris Dataset which is plot distributions for each column of a wide-form dataset:
Iris data set consists of 3 different types of irises’ (Setosa, Versicolour, and Virginica) petal and sepal length, stored in a 150×4 numpy.ndarray
Loading the iris dataset for Kdeplot:
Python3
iris = sns.load_dataset('iris')iris
Output:
Once we have species set then if we want to simply calculate the petal_length and petal_width then Simple pass the two variables(Setosa and verginica ) into the seaborn.kdeplot() methods.
Python3
setosa = iris.loc[iris.species=="setosa"]virginica = iris.loc[iris.species == "virginica"]sns.kdeplot(setosa.petal_length, setosa.petal_width)
Output:
See another example if we want to calculate another variable attribute which is sepal_width and sepal_length.
Python3
sns.kdeplot(setosa.sepal_width, setosa.sepal_length)
Output:
If we pass the two separate Kdeplot with different variable:
Python3
sns.kdeplot(setosa.petal_length, setosa.petal_width)sns.kdeplot(virginica.petal_length, virginica.petal_width)
Output:
Python-Seaborn
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Python map() function
Adding new column to existing DataFrame in Pandas
Python Dictionary
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Taking input in Python
Enumerate() in Python
Read a file line by line in Python
Python String | replace()