Home » Python » Plot Multiple Variables On Density Plot in Python

# Plot Multiple Variables On Density Plot in Python

Use the function kdeplot() of the seaborn library to draw a density plot in Python.

In this article, we will discuss how to plot multiple variables on a density plot like two variables density plot and three variables density plot.

Density plots require a numeric values dataset to display the plot in python.

## Installation of Packages

We will need a seaborn package to load the dataset and plot the density graph in the python program. If you don’t have these packages installed on your system, install them using the 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 a single axis in python using the seaborn library.

### Installation of Packages

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

`pip install seaborn`

### Import libraries

Import the seaborn library in our python density plot code.

```# Import library

import seaborn as sns```

### Prepare dataset

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

1. Load tips dataset using `load_dataset()` function of the seaborn library in the df variable
2. We will use the `total_bill` and `tip` column from the tips dataset.
```# Load the tips dataset

### Plot Density Graph using kdeplot()

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

1. The first argument in kdeplot() defines the dataset to use for the plot.
2. The second argument defines shade as true.
3. The third argument defines color as red for the `total_bill` column and blue color for the `tip` column dataset.
```# Plot 2 variables on Density Graph

### Two Variables on Density Plot Python Code

Use entire density python code below using the seaborn library

```# library and dataset
import seaborn as sns

# plot of 2 variables on Density Graph
```

### Two Variables on Density Plot Python Visualization Output

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

## Plot Three Variables in Density Plot

Let’s see an example to plot three variable density plot in a single axis in python using the seaborn library.

### Installation of Packages

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

`pip install seaborn`

### Import libraries

Import the seaborn library in our python density plot code.

```# Import library

import seaborn as sns```

### Prepare dataset

Seaborn library has inbuilt datasets. We will use the `penguins` dataset to visualize the density plot

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

### Plot Density Graph using kdeplot()

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

• The first argument `kdeplot()` defines the dataset to use for the plot
• The second argument defines shade as `true`
• The third argument defines color as red for the `total_bill` column and blue color for the `tip` column dataset.
```# plot 3 variables on density graph

### Three Variables on Density Plot Python Code

Use entire density python code below using the seaborn library

```# library and dataset
import seaborn as sns

# plot 3 variables on density graph