Plot Multiple Variables On Density Plot in Python

In this article, I will explain you how to plot multiple variables on density plot like two variables density plot and three variables density plot. Density plot require numeric values dataset to display plot in python.

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 histogram with several variables in python!

Plot Multiple Variables in Density Plot

Let’s see an example to plot two variable density plot in single axis 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
  • We will use total_bill and tip column from tips dataset.
# Load the tips dataset
df = sns.load_dataset('tips')

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
  • The second argument defines shade as true
  • The third argument defines color as red for total_bill column and blue color for tip column dataset.
# Plot 2 variables on Density Graph
p1=sns.kdeplot(df['total_bill'], shade=True, color="r")
p1=sns.kdeplot(df['tip'], shade=True, color="b")

Two Variables on Density Plot Python Code

Use below entire density python code using seaborn library

# library and dataset
import seaborn as sns
df = sns.load_dataset('tips')
 
# plot of 2 variables on Density Graph
p1=sns.kdeplot(df['total_bill'], shade=True, color="r")
p1=sns.kdeplot(df['tip'], shade=True, color="b")

Two Variables on Density Plot Python Visualization Output

Two Variable on Density Plot x-axis Python
Two Variable on Density Plot x-axis Python

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

Plot Three Variables in Density Plot

Let’s see an example to plot three variable density plot in single axis 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 penguins dataset to visualize density plot

  • Load tips dataset using load_dataset() function of seaborn library in df variable
  • We will use bill_length_mm, bill_depth_mm and flipper_length_mm columns from penguins dataset.
# Load the tips dataset
df = sns.load_dataset('penguins')

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
  • The second argument defines shade as true
  • The third argument defines color as red for total_bill column and blue color for tip column dataset.
# plot 3 variables on density graph
p1=sns.kdeplot(df['bill_length_mm'], shade=True, color="r")
p1=sns.kdeplot(df['bill_depth_mm'], shade=True, color="b")
p1=sns.kdeplot(df['flipper_length_mm'], shade=True, color="g")

Three Variables on Density Plot Python Code

Use below entire density python code using seaborn library

# library and dataset
import seaborn as sns
df = sns.load_dataset('penguins')
 
# plot 3 variables on density graph
p1=sns.kdeplot(df['bill_length_mm'], shade=True, color="r")
p1=sns.kdeplot(df['bill_depth_mm'], shade=True, color="b")
p1=sns.kdeplot(df['flipper_length_mm'], shade=True, color="g")

Three Variables on Density Plot Python Visualization Output

Three Variables on Density Plot - Python
Three Variables on Density Plot – Python

Cool Tip: Know more about how to plot customized stacked area graph in python!

Conclusion

I hope you found above article to plot multiple variables on 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.