Table of Contents
Matplotlib is a popular Python library used for data visualization. One of its features is the ability to create histograms, which are graphical representations of data distribution. In Matplotlib, the bin size refers to the width of the bars in a histogram and can greatly affect the interpretation of the data. To adjust the bin size in Matplotlib histograms, one can use the “bins” parameter in the plt.hist() function. This parameter allows the user to specify the number of bins or the width of each bin, giving them control over the granularity of the histogram. By adjusting the bin size, the user can fine-tune the visualization to better understand the data distribution and make more accurate data-driven decisions.
Adjust Bin Size in Matplotlib Histograms
You can use one of the following methods to adjust the bin size of histograms in Matplotlib:
Method 1: Specify Number of Bins
plt.hist(data, bins=6)
Method 2: Specify Bin Boundaries
plt.hist(data, bins=[0, 4, 8, 12, 16, 20])
Method 3: Specify Bin Width
w=2 plt.hist(data, bins=np.arange(min(data), max(data) + w, w))
The following examples show how to use each of these methods in practice.
Example 1: Specify Number of Bins
The following code shows how to specify the number of bins to use in a histogram:
import matplotlib.pyplotas plt
#define data
data = [1, 2, 2, 4, 5, 5, 6, 8, 9, 12, 14, 15, 15, 15, 16, 17, 19]
#create histogram with specific number of bins
plt.hist(data, edgecolor='black', bins=6)

Keep in mind that the more bins you specify, the more narrow the bins will be.
Example 2: Specify Bin Boundaries
The following code shows how to specify the actual bin boundaries in a histogram:
import matplotlib.pyplotas plt
#define data
data = [1, 2, 2, 4, 5, 5, 6, 8, 9, 12, 14, 15, 15, 15, 16, 17, 19]
#create histogram with specific bin boundaries
plt.hist(data, edgecolor='black', bins=[0, 4, 8, 12, 16, 20])
Example 3: Specify Bin Width
The following code shows how to specify the bin width in a histogram:
import matplotlib.pyplotas plt import numpy as np #define data data = [1, 2, 2, 4, 5, 5, 6, 8, 9, 12, 14, 15, 15, 15, 16, 17, 19] #specify bin width to use w=2 #create histogram with specified bin width plt.hist(data, edgecolor='black', bins=np.arange(min(data), max(data) + w, w))

Keep in mind that the smaller the bin width you specify, the more narrow the bins will be.
The following tutorials explain how to perform other common functions in Matplotlib:
Cite this article
stats writer (2024). How can I adjust the bin size in Matplotlib histograms?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-adjust-the-bin-size-in-matplotlib-histograms/
stats writer. "How can I adjust the bin size in Matplotlib histograms?." PSYCHOLOGICAL SCALES, 6 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-adjust-the-bin-size-in-matplotlib-histograms/.
stats writer. "How can I adjust the bin size in Matplotlib histograms?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-adjust-the-bin-size-in-matplotlib-histograms/.
stats writer (2024) 'How can I adjust the bin size in Matplotlib histograms?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-adjust-the-bin-size-in-matplotlib-histograms/.
[1] stats writer, "How can I adjust the bin size in Matplotlib histograms?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I adjust the bin size in Matplotlib histograms?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
