0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Set Bins in Histogram in Matplotlib: Simple Guide

In matplotlib, you set bins in a histogram by passing the bins parameter to plt.hist(). You can specify an integer for the number of equal-width bins or provide a list of bin edges to control exact bin ranges.
๐Ÿ“

Syntax

The basic syntax to set bins in a histogram using matplotlib is:

  • plt.hist(data, bins=number_or_sequence)

Here, data is your dataset, and bins controls how the data is grouped:

  • If bins is an integer, it defines the number of equal-width bins.
  • If bins is a sequence (list or array), it defines the bin edges explicitly.
python
plt.hist(data, bins=10)
plt.hist(data, bins=[0, 5, 10, 15])
๐Ÿ’ป

Example

This example shows how to create histograms with different bin settings: one with 5 equal bins and another with custom bin edges.

python
import matplotlib.pyplot as plt
import numpy as np

# Sample data: 100 random numbers from 0 to 20
np.random.seed(0)
data = np.random.uniform(0, 20, 100)

plt.figure(figsize=(10,4))

# Histogram with 5 equal-width bins
plt.subplot(1, 2, 1)
plt.hist(data, bins=5, color='skyblue', edgecolor='black')
plt.title('Histogram with 5 bins')

# Histogram with custom bins
custom_bins = [0, 4, 8, 12, 16, 20]
plt.subplot(1, 2, 2)
plt.hist(data, bins=custom_bins, color='salmon', edgecolor='black')
plt.title('Histogram with custom bins')

plt.tight_layout()
plt.show()
Output
Two side-by-side histograms: left with 5 equal bins, right with bins at edges 0,4,8,12,16,20
โš ๏ธ

Common Pitfalls

Common mistakes when setting bins include:

  • Using a non-integer or invalid type for bins (must be int or sequence).
  • Providing bin edges that do not cover the full data range, causing some data points to be excluded.
  • Setting too few bins, which oversimplifies data, or too many bins, which overcomplicates the histogram.

Always check your data range and choose bins accordingly.

python
import matplotlib.pyplot as plt
import numpy as np

data = np.random.normal(size=100)

# Wrong: bins as float (will raise error)
# plt.hist(data, bins=5.5)

# Wrong: bins edges not covering data range
# plt.hist(data, bins=[-1, 0, 1])  # data outside this range is ignored

# Right: bins covering full range
plt.hist(data, bins=[data.min(), 0, data.max()])
plt.show()
Output
Histogram plot showing data grouped into bins covering full data range
๐Ÿ“Š

Quick Reference

ParameterDescriptionExample
bins=intNumber of equal-width binsbins=10
bins=sequenceExplicit bin edgesbins=[0, 5, 10, 15]
bins='auto'Automatic bin size selectionbins='auto'
bins='fd'Freedman-Diaconis estimator for bin widthbins='fd'
โœ…

Key Takeaways

Use the bins parameter in plt.hist() to control histogram binning.
An integer bins value creates that many equal-width bins.
A list of bin edges lets you define exact bin boundaries.
Ensure bin edges cover your entire data range to include all points.
Avoid invalid bins types and choose bin count to balance detail and clarity.