Bin Size in Matplotlib Histogram
Bin size in a Matplotlib histogram controls how data is grouped into bins, each bin covers a value range and its height shows the count of data points in that range. Smaller bin sizes give more detailed distributions with many bins, while larger sizes produce fewer bins and a simpler view. For example, bins=5 divides the data into five equal parts for an easy-to-read summary.
Using integer bins
When you pass an integer to the bins parameter in Matplotlib, it automatically divides the entire range of data into that many equal-width bins. This approach allows for quick and simple visualizations without needing to manually specify bin edges or widths.
import matplotlib.pyplot as plt
a = [189, 185, 195, 149, 189, 147, 154,
174, 169, 195, 159, 192, 155, 191,
153, 157, 140, 144, 172, 157, 181,
182, 166, 167]
plt.hist(a, bins=5, edgecolor="red")
plt.title("Histogram with 5 bins")
plt.show()
Output

Explanation: Setting bins=5 divides the data range into 5 equal-width intervals. Matplotlib calculates the bin width and counts how many values fall into each bin to set bar heights. edgecolor="red" highlights bar borders for clarity.
Using list of edges
You manually define each bin edge using a list. This gives full control over bin ranges, including unequal widths. Useful when data needs custom grouping or uneven intervals.
Example 1: Equal width bins using custom edges
import matplotlib.pyplot as plt
a = [1, 2, 3, 2, 1, 2, 3, 2,
1, 4, 5, 4, 3, 2, 5, 4,
5, 4, 5, 3, 2, 1, 5]
plt.hist(a, bins=[1, 2, 3, 4, 5], edgecolor="black")
plt.title("Equal width bins using custom edges")
plt.show()
Output

Explanation: Setting bins=[1, 2, 3, 4, 5] defines custom bin edges, creating equal-width bins of size 1. Matplotlib groups values based on these edges and counts their frequency for bar heights. edgecolor="black" outlines the bars for clear distinction.
Example 2: Unequal width bins using custom edges
import matplotlib.pyplot as plt
a = [189, 185, 195, 149, 189, 147,
154, 174, 169, 195, 159, 192,
155, 191, 153, 157, 140, 144,
172, 157, 181, 182, 166, 167]
plt.hist(a, bins=[140, 150, 160, 175, 185, 200],
edgecolor="yellow", color="grey")
plt.title("Unequal width bins using custom edges")
plt.show()
Output

Explanation: bins list [140, 150, 160, 175, 185, 200] sets custom bin edges with unequal widths. Matplotlib counts how many values fall into each interval. edgecolor="yellow" and color="grey" style the histogram for visual appeal.
Using range()
You define the bin width explicitly using range(). It’s simple and ideal for equal bin spacing and readability. Great when you want consistent intervals across your data.
import matplotlib.pyplot as plt
a = [87, 53, 66, 61, 67, 68, 62, 110,
104, 61, 111, 123, 117, 119, 116,
104, 92, 111, 90, 103, 81, 80, 101,
51, 79, 107, 110, 129, 145, 128,
132, 135, 131, 126, 139, 110]
binwidth = 8
plt.hist(a, bins=range(min(a), max(a) + binwidth, binwidth),
edgecolor="yellow", color="brown")
plt.title("Histogram with bin width = 8 using range()")
plt.show()
Output

Explanation: bins=range(min(a), max(a)+binwidth, binwidth) creates evenly spaced bins of width 8. This gives consistent intervals and edgecolor="yellow" with color="brown" helps differentiate each bar clearly.
Using automatic bin selection
This method lets Matplotlib choose the optimal number of bins based on the data. It's great for large datasets or when you're unsure about bin size. Use strategies like 'fd', 'sturges', or 'auto' for best-fit results.
from re import A
import matplotlib.pyplot as plt
import numpy as np
a = np.random.randn(1000)
plt.hist(a, bins='fd', edgecolor='blue')
plt.title("Histogram with 'fd' automatic bin selection")
plt.show()
Output

Explanation: bins='fd' applies the Freedman–Diaconis rule for automatic bin width calculation. This adjusts bin size based on data spread. edgecolor='blue' makes bar edges stand out.