Open In App

Seaborn Kdeplot – A Comprehensive Guide

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 and bivariate scenarios for efficient data visualization.

What is KDE plot?

Kernel Density Estimate (KDE) Plot allows 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

How to visualize KDE Plot using Seaborn?

We learn the usage of some parameters through some specific examples:

Importing Libraries

First import the corresponding library

Python3




import pandas as pd
import seaborn as sb
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline


Draw a simple one-dimensional kde image:

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 seaborn
x= np.random.randn(200)
y = np.random.randn(200)
 
# Kde for x var
sns.kdeplot(x)


Output:

Then after check for y-axis.

Python3




sns.kdeplot(y)


Output:

Use Shade to fill the area covered by curve:

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:

Use Vertical to draw indicates whether to draw on the X axis or on the Y axis

Python3




sns.kdeplot(x, vertical = True)


Output:

Bivariate Kdeplot for two variables: 

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:

KDE Plot of Iris Dataset

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:

Bivariate Kdeplot for two variables of iris:

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 virginica ) 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:

Conclusion

In summary, kdeplot in Seaborn offers a versatile approach to visualize probability density functions, aiding in the exploration of one or more dimensions in datasets. Whether shading areas, adjusting colors, or applying it to real-world datasets like Iris, kdeplot stands as a valuable tool for data scientists and analysts.

Frequently Asked Questions (FAQs)

1.What is KDE plot used for?

KDE plot is used to estimate the probability density function of a continuous variable, providing insights into data distribution, shape, and central tendency.

2.What is a 2-dimensional KDE plot?

A 2-dimensional KDE plot visualizes the joint probability density of two continuous variables, offering insights into their bivariate distribution and correlation.



Previous Article
Next Article

Similar Reads

Seaborn Heatmap - A comprehensive guide
Heatmap is defined as a graphical representation of data using colors to visualize the value of the matrix. In this, to represent more common values or higher activities brighter colors basically reddish colors are used and to represent less common or activity values, darker colors are preferred. Heatmap is also defined by the name of the shading m
6 min read
Managing Jupyter Kernels: A Comprehensive Guide
Jupyter Notebooks have completely transformed how we go about working with code and data by providing a flexible platform that supports multiple programming languages. These notebooks are a valuable tool for data scientists and developers to create and share documents that combine code (e.g. Python) with rich text elements (such as images, links, e
10 min read
Comparing Word2Vec, Sentence2Vec, and Doc2Vec: A Comprehensive Analysis
Answer: Word2Vec focuses on word-level embeddings, Sentence2Vec on sentence-level embeddings, and Doc2Vec on document-level embeddings, catering to different granularities of text representation.Below is a summarized comparison of Word2Vec, Sentence2Vec, and Doc2Vec across various key aspects. FeatureWord2VecSentence2VecDoc2VecGranularityWord-level
1 min read
Horizontal Boxplots with Seaborn in Python
Prerequisite: seaborn The Boxplots are used to visualize the distribution of data which is useful when a comparison of data is required. Sometimes, Boxplot is also known as a box-and-whisker plot. The box shows the quartiles of dataset and whiskers extend to show rest of the distribution. In this article, we are going to implement the Horizontal bo
1 min read
How to Show Mean on Boxplot using Seaborn in Python?
A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles: median: the middle value of data. marked as Q2, portrays the 50th percentile.first quartile: the middle value between "minimum non-outlier" and median.
2 min read
Change Axis Labels, Set Title and Figure Size to Plots with Seaborn
Seaborn is Python's visualization library built as an extension to Matplotlib. Seaborn has Axes-level functions (scatterplot, regplot, boxplot, kdeplot, etc.) as well as Figure-level functions (lmplot, factorplot, jointplot, relplot etc.). Axes-level functions return Matplotlib axes objects with the plot drawn on them while figure-level functions i
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 install it manually. Plotting categorical scatter plo
8 min read
Boxplot using Seaborn in Python
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. Box Plot A box plot helps to maintain the distribut
5 min read
How to set a Seaborn chart figure size?
Seaborn is a Python data visualization library based on Matplotlib. It is used to draw attractive and informative statistical graphics. To adjust the figure size of the seaborn plot we will use the subplots function of matplotlib.pyplot. Examples to change the figure size of a seaborn axes matplotlib.pyplot.subplots() Create a figure and a set of s
2 min read
Box plot visualization with Pandas and Seaborn
Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used for detect the outlier in data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Boxplot summarizes a sample data using 25th, 50th and 75th per
2 min read
Practice Tags :