Skip to content
Related Articles

Related Articles

Density Plots with Pandas in Python

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 26 Nov, 2020

Density Plot is a type of data visualization tool. It is a variation of the histogram that uses ‘kernel smoothing’ while plotting the values. It is a continuous and smooth version of a histogram inferred from a data.

Density plots uses Kernel Density Estimation (so they are also known as Kernel density estimation plots or KDE) which is a probability density function. The region of plot with a higher peak is the region with maximum data points residing between those values.

Density plots can be made using pandas, seaborn, etc. In this article, we will generate density plots using Pandas. We will be using two datasets of the Seaborn Library namely – ‘car_crashes’ and ‘tips’. 

Syntax: pandas.DataFrame.plot.density | pandas.DataFrame.plot.kde

where pandas -> the dataset of the type ‘pandas dataframe’

Dataframe -> the column for which the density plot is to be drawn

plot -> keyword directing to draw a plot/graph for the given column

density -> for plotting a density graph 

kde -> to plot a density graph using the Kernel Density Estimation function

Example 1: Given the dataset ‘car_crashes’, let’s find out using the density plot which is the most common speed due to which most of the car crashes happened.


# importing the libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# loading the dataset
# from seaborn library
data = sns.load_dataset('car_crashes')
# viewing the dataset


Plotting the graph:


# plotting the density plot 
# for 'speeding' attribute
# using plot.density()
plt.title('Density plot for Speeding')


Using a density plot, we can figure out that the speed between 4-5 (kmph) was the most common for crash crashes in the dataset because of it being high density (high peak) region.

Example 2: For another dataset ‘tips’, let’s calculate what was the most common tip given by a customer. 


# loading the dataset
# from seaborn library
data = sns.load_dataset('tips')
# viewing the dataset


‘tips’ dataset

Plotting the graph:


# density plot for 'tip'
plt.title('Density Plot for Tip')

Through the above density plot, we can infer that the most common tip that was given was in the range of 2.5 – 3. The highest peak/density (as represented on the y-axis) was found to be at the tip value of 2.5 – 3. 

Plotting the above plot using the plot.kde()

KDE or the Kernel Density Estimation uses Gaussian Kernels to estimate the Probability Density Function of a random variable. Below is the implementation of plotting the density plot using kde() for the dataset ‘tips’.


# for 'tip' attribute
# using plot.kde()
plt.title('KDE-Density plot for Tip')

Using this we can infer that there is no major difference between plot.density() and plot.kde() and can be therefore used interchangeably.

Density plots have an advantage over Histograms because they determine the Shape of the distribution more efficiently than histograms. They do not have to depend on the number of bins used unlike in histograms.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!