0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Density Plot in Matplotlib: Simple Guide

To create a density plot in matplotlib, use the hist() function with the parameter density=True to normalize the histogram. Alternatively, use seaborn.kdeplot() for a smooth kernel density estimate plot.
๐Ÿ“

Syntax

The basic syntax to create a density plot using matplotlib is:

  • plt.hist(data, bins, density=True): Plots a normalized histogram that represents the density.
  • data: The input numeric data array.
  • bins: Number of bins or bin edges to group the data.
  • density=True: Normalizes the histogram so the area sums to 1, showing probability density.

For a smooth density curve, you can use seaborn.kdeplot(data) which plots a kernel density estimate.

python
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.random.normal(size=1000)

# Density plot using hist
plt.hist(x, bins=30, density=True)
plt.title('Density Plot using matplotlib.hist')
plt.show()
Output
A histogram plot with 30 bins showing the density (area under bars sums to 1).
๐Ÿ’ป

Example

This example shows how to create a density plot using matplotlib and also a smooth density curve using seaborn.

python
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# Generate sample data
np.random.seed(0)
data = np.random.normal(loc=0, scale=1, size=1000)

# Density plot with matplotlib
plt.hist(data, bins=40, density=True, alpha=0.5, label='Histogram Density')

# Smooth density plot with seaborn
sns.kdeplot(data, color='red', label='KDE')

plt.title('Density Plot with matplotlib and seaborn')
plt.xlabel('Value')
plt.ylabel('Density')
plt.legend()
plt.show()
Output
A plot showing a semi-transparent histogram density and a smooth red KDE curve overlay.
โš ๏ธ

Common Pitfalls

  • Forgetting density=True in plt.hist() results in counts, not density.
  • Using too few bins can hide data details; too many bins can make the plot noisy.
  • Confusing density=True with normed=True (the latter is deprecated).
  • Not installing or importing seaborn when trying to use kdeplot.
python
import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(size=1000)

# Wrong: missing density=True
plt.hist(x, bins=30)
plt.title('Histogram without density=True')
plt.show()

# Correct: with density=True
plt.hist(x, bins=30, density=True)
plt.title('Histogram with density=True')
plt.show()
Output
First plot shows counts on y-axis; second plot shows density normalized to area 1.
๐Ÿ“Š

Quick Reference

FunctionPurposeKey Parameters
plt.hist()Plot histogram or density plotdata, bins, density=True
sns.kdeplot()Plot smooth kernel density estimatedata, shade=True, bw_adjust
np.random.normal()Generate sample normal dataloc, scale, size
โœ…

Key Takeaways

Use plt.hist(data, density=True) to create a density plot in matplotlib.
Seaborn's kdeplot provides a smooth density curve for better visualization.
Always set density=True to normalize histogram to probability density.
Choose an appropriate number of bins to balance detail and noise.
Avoid deprecated parameters like normed=True; use density=True instead.