> For the complete documentation index, see [llms.txt](https://vecco-insight.gitbook.io/crash-visualization/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vecco-insight.gitbook.io/crash-visualization/seaborn/untitled-3/4.5.4-density-plot.md).

# 4.5.4 Density plot

Density plots are a commonly used tool to visualize the distribution of a continuous variable.

```
mpg = sns.load_dataset('mpg')   # load  the  embedded  dataset
```

### 1. Simple Density Chart

```
# Draw Density Plot

sns.kdeplot(mpg.loc[mpg["cylinders"] == 4, "mpg"], shade=True, color="g", label="Cyl=4", alpha=.7)
sns.kdeplot(mpg.loc[mpg["cylinders"] == 5, "mpg"], shade=True, color="deeppink", label= "Cyl=5", alpha=.7)
sns.kdeplot(mpg.loc[mpg["cylinders"] == 6, "mpg"], shade=True, color="dodgerblue", label= "Cyl=6", alpha=.7)
sns.kdeplot(mpg.loc[mpg["cylinders"] == 8, "mpg"], shade=True, color="orange", label= "Cyl=8", alpha=.7)
plt.title("Density Plot of MPG by n_Cylinders")
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBhAMgZHSAsfKwFOe2w%2F-MBho8HeN966cZdgYhCS%2Fdensity%20plot%201.png?alt=media\&token=ab63a6f6-7ad3-490c-a11a-c3bdbd2ffdfb)

```
sns.distplot(mpg.loc[mpg['origin'] == 'usa', 'horsepower'],  color='g', label='USA', hist_kws={'alpha':.6})
sns.distplot(mpg.loc[mpg['origin'] == 'europe', 'horsepower'],  color='deeppink', label='Europe', hist_kws={'alpha':.6})
sns.distplot(mpg.loc[mpg['origin'] == 'japan', 'horsepower'], color='dodgerblue', label='Japan', hist_kws={'alpha':.6})
plt.title('Density Plot of Horsepower by origins')
plt.legend()
```

### 2. Density Curves with Histogram <a href="#id-23.-density-curves-with-histogram" id="id-23.-density-curves-with-histogram"></a>

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBhAMgZHSAsfKwFOe2w%2F-MBhoHsfstRBvlcoDtJ9%2Fdownload.png?alt=media\&token=3f274eae-4005-41b7-9155-a4b8d0c2492b)

### 3. Bandwidth Control  (Bins)

Just like [`bins`](https://app.gitbook.com/@ivy-wang/s/crash-visulisation/~/drafts/-MBipx4bc8WGYUTo-gGD/matplotlib/1.4-histogram-plot#bins) in `matplotlib`, in seaborn, this is controlled using the **`bw`** argument of the **`kdeplot`** function.

```
sns.kdeplot(mpg.loc[mpg["cylinders"] == 6, "mpg"], shade=True, color="g", label="bw: 0.1", alpha=.7,bw=.1)
sns.kdeplot(mpg.loc[mpg["cylinders"] == 6, "mpg"], shade=True, color="b", label="bw: 0.5", bw=.5)
plt.title("With  bandwidth control")
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBif4He74q-nhNx9227%2F-MBiqWx7IjtIIH3RChq-%2Fbandwidth%20control.png?alt=media\&token=3a0ae0d7-33b9-4f3e-a720-a78004df9eca)
