How to Create Density Plot in Python

What is Density Plot ?

Density plot are the representation of numerical values using kernel density probabilities. Density plot require numeric values dataset to display plot. In this article, I will explain you how to create density plot in python, density plot with shade and create vertical density plot.

To draw density plot in python use kdeplot() function of seaborn library.

Installation of Packages

We will need seaborn package to load dataset and plot density graph in python program. If you don’t have these packages installed on your system, install it using below commands.

pip install seaborn

Cool Tip: Know more about how to plot line graph in python!

How to Create Density Plot in Python using seaborn

Let’s see an example to draw density plot in python using seaborn library.

Installation of Packages

We will need seaborn package to show density plot. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python density plot code.

# Import library

import seaborn as sns

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize density plot

  • Load tips dataset using load_dataset() function of seaborn library in df variable
# Load the tips dataset
df = sns.load_dataset('tips')

Cool Tip: Learn how to plot pie chart in python!

Plot Density Graph using kdeplot()

Use sns.kdeplot()function of seaborn module to draw density graph.

The first argument in kdeplot() defines dataset use for plot

# Plot Density Graph
sns.kdeplot( df["total_bill"] )

Density Plot Python Code

Use below entire density python code using seaborn library

# Import library
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')

# Plot Density Graph
sns.kdeplot( df["total_bill"] )

Density Plot Python Visualization Output

Basic Density Plot in Python
Basic Density Plot in Python

Cool Tip: Learn how to plot histogram chart in python!

Create Density Plot with Shade in Python

Let’s see an example to plot density plot in python with shade.

Installation of Packages

We will need seaborn package to plot density graph. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python density plot code.

# Import library
import seaborn as sns

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize density graph

  • Load tips dataset using load_dataset() function of seaborn library in df variable
# Load the tips dataset
df = sns.load_dataset('tips')

Plot Density Graph using kdeplot()

Use sns.kdeplot() function of seaborn module to draw density plot with shade.

  • The first argument in kdeplot() defines dataset use to plot density.
  • The second argument in kdeplot() defines shade = True

# Plot Density Graph with shade
sns.kdeplot(df["total_bill"],shade=True )

Density Plot with Shade Python Code

Use below entire density plot python code using seaborn library

# Import library
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')

# Plot Density Graph with shade
sns.kdeplot(df["total_bill"],shade=True )

Python Density Plot with Shade Visualization Output

Density Plot with Shade - Python
Density Plot with Shade – Python

Cool Tip: Learn how to plot Treemap chart in python!

Plot Vertical Density Plot with Shade

Let’s see an example to plot vertical density plot with shade in python.

Installation of Packages

We will need seaborn package to plot density plot with shade. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python vertical density plot code.

# Import library
import seaborn as sns

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize density plot.

  • Load tips dataset using load_dataset() function of seaborn library in df variable
# Load the tips dataset
df = sns.load_dataset('tips')

Plot Vertical Density Graph with Shade using kdeplot()

Use sns.kdeplot() function of seaborn module to draw vertical density plot.

  • The first argument in kdeplot() function defines dataset used for density plot.
  • The second argument in kdeplot() function defines shade as True
  • The third argument in kdeplot() function defines vertical as True to display vertical density plot.
# vertical density plot with shade
sns.kdeplot(df['total_bill'], shade=True, vertical=True)

Python Density Graph Vertical Code

Use below entire python vertical density plot code using seaborn library

# Import library and dataset
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')
 
# vertical density plot with shade
sns.kdeplot(df['total_bill'], shade=True, vertical=True)

Python Vertical Density Plot with Shade Visualization Output

Vertical Density Plot with Shade - Python
Vertical Density Plot with Shade – Python

Cool Tip: Learn how to plot stacked area chart in python!

Conclusion

I hope you found above article on create density plot in python program using seaborn package informative and educational.

Use sns.kdeplot() function of seaborn module to create density plot in python program. Using shade and vertical parameter, you can customize appearance of density plot.