0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Multiple Histograms in Matplotlib Easily

To create multiple histograms in matplotlib, use the plt.hist() function multiple times with different data sets before calling plt.show(). You can also plot them side-by-side by specifying the alpha for transparency and label for legends to compare distributions clearly.
๐Ÿ“

Syntax

The basic syntax to create multiple histograms in matplotlib is:

  • plt.hist(data1, bins=10, alpha=0.5, label='label1'): Plots the first histogram.
  • plt.hist(data2, bins=10, alpha=0.5, label='label2'): Plots the second histogram on the same axes.
  • plt.legend(): Adds a legend to distinguish histograms.
  • plt.show(): Displays the plot.

Parameters explained:

  • data: The dataset to plot.
  • bins: Number of bins or bin edges.
  • alpha: Transparency level (0 to 1) to see overlapping bars.
  • label: Name shown in the legend.
python
plt.hist(data1, bins=10, alpha=0.5, label='Data 1')
plt.hist(data2, bins=10, alpha=0.5, label='Data 2')
plt.legend()
plt.show()
๐Ÿ’ป

Example

This example shows how to plot two histograms on the same plot to compare two data sets. Transparency is set so both histograms are visible.

python
import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
np.random.seed(0)
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1.5, 1000)

# Plot histograms
plt.hist(data1, bins=30, alpha=0.6, label='Normal(0,1)')
plt.hist(data2, bins=30, alpha=0.6, label='Normal(2,1.5)')

plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Multiple Histograms Example')
plt.legend()
plt.show()
Output
A plot window showing two overlapping histograms with different centers and spreads, each labeled and semi-transparent.
โš ๏ธ

Common Pitfalls

Common mistakes when creating multiple histograms include:

  • Not setting alpha, which makes overlapping bars hard to see.
  • Forgetting to add label and plt.legend(), so histograms are not distinguishable.
  • Using different bins sizes for each histogram, which can mislead comparison.
  • Plotting histograms separately without the same axes, resulting in multiple plots instead of one combined view.
python
import matplotlib.pyplot as plt
import numpy as np

# Wrong way: no alpha, no labels
np.random.seed(0)
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1.5, 1000)
plt.hist(data1, bins=30)
plt.hist(data2, bins=30)
plt.show()

# Right way: with alpha and labels
plt.hist(data1, bins=30, alpha=0.6, label='Data 1')
plt.hist(data2, bins=30, alpha=0.6, label='Data 2')
plt.legend()
plt.show()
Output
First plot: two solid histograms overlapping with no transparency and no legend, hard to distinguish. Second plot: two semi-transparent histograms with legend, easy to compare.
๐Ÿ“Š

Quick Reference

Tips for creating multiple histograms in matplotlib:

  • Use the same bins for all histograms to keep comparison fair.
  • Set alpha between 0.4 and 0.7 for good visibility.
  • Always add label and call plt.legend() to identify histograms.
  • Use plt.figure(figsize=(width, height)) to adjust plot size if needed.
โœ…

Key Takeaways

Use plt.hist multiple times with different data and set alpha for transparency to create multiple histograms on one plot.
Always add labels and call plt.legend() to distinguish histograms clearly.
Keep the same bins for all histograms to make comparisons accurate.
Avoid plotting histograms separately if you want to compare them visually on the same axes.