๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ api โ€บ _as_gen โ€บ matplotlib.pyplot.hist.html
matplotlib.pyplot.hist โ€” Matplotlib 3.11.2 documentation
Location of the bottom of each bin, i.e. bins are drawn from bottom to bottom + hist(x, bins) If a scalar, the bottom of each bin is shifted by the same amount. If an array, each bin is shifted independently and the length of bottom must match the number of bins. If None, defaults to 0. histtype{'bar', 'barstacked', 'step', 'stepfilled'}, default: 'bar' The type of histogram to draw.
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ statistics โ€บ histogram_normalization.html
Histogram bins, density, and weight โ€” Matplotlib 3.11.2 documentation
Histograms are created by defining bin edges, and taking a dataset of values and sorting them into the bins, and counting or summing how much data is in each bin. In this simple example, 9 numbers between 1 and 4 are sorted into 3 bins:
Discussions

python - How to choose bins in matplotlib histogram - Stack Overflow
Can someone explain to me what "bins" in histogram are (the matplotlib hist function)? And assuming I need to plot the probability density function of some data, how do the bins I choose influence ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Bin size in Matplotlib (Histogram) - Stack Overflow
I'm using matplotlib to make a histogram. Is there any way to manually set the size of the bins as opposed to the number of bins? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to add bin labels to histogram in matplotlib?
\1. Use plt.xticks: >>> bins = [1, 25, 45, 55, 80] >>> plt.hist(..., bins=bins) >>> plt.xtcks(bins) But is that actually a thing? The bins in a histogram are usually evenly spaced... 2. Use plt.annotate: >>> y = ... >>> bins = [1, 25, 45, 55, 80] >>> plt.hist(y, ...) >>> for xi, yi in zip(bins, y) ... plt.annotate(str(y), (xi, yi)) ... To adjust the alignment of each annotation relative to its x-y coordinates, use the ha (horizontal) and va (vertical) kwargs until it looks right. Also, why all the _ = ...? Your plot will show up just fine without assigning those return values to variables. More on reddit.com
๐ŸŒ r/learnpython
6
2
April 2, 2021
How to give matplotlib histogram a list or bin ranges?
As a janky workaround, you can do that by, say, bins = [0,2,5,10] and data = [0,0,2,2,2,2,5,5,5,5,5,5,5,5,5]. Write a function that wraps plt.hist accordingly. Remember that matplotlib always treats the left endpoint of bins as inclusive. More on reddit.com
๐ŸŒ r/learnpython
1
1
August 24, 2022
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ bin-size-in-matplotlib-histogram
Bin Size in Matplotlib Histogram - GeeksforGeeks
July 23, 2025 - 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.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ histograms-matplotlib
Histograms in Matplotlib | DataCamp
June 17, 2019 - For example: Let's say, you would like to plot a histogram of an RGB image having pixel values ranging from $0$ to $255$. The maximum of bins this image can have is $255$. If you keep x_axis (number of bins) as $255$, then the y_axis will denote the frequency of each pixel in that image.
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ how-to-create-a-matplotlib-histogram
How to Create a Matplotlib Histogram? - StrataScratch
March 10, 2025 - This code will create a histogram with 10 bins. ... You can also provide a sequence of bin edges, specifying each bin size. For example, this will create a histogram with seven bins.
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 04.05-histograms-and-binnings.html
Histograms, Binnings, and Density | Python Data Science Handbook
%matplotlib inline import numpy ... display; here's an example of a more customized histogram: ... plt.hist(data, bins=30, normed=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none');...
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ data-visualization-matplotlib-seaborn โ€บ chapter-3-basic-matplotlib-plot-types โ€บ matplotlib-histogram-bins
Understanding Histogram Bins in Matplotlib
Histogram using 50 bins. This shows excessive detail and noise, making the underlying pattern harder to discern. You can control the bins in plt.hist() using the bins argument: Specify the Number of Bins: Pass an integer to the bins argument. Matplotlib will then create that many bins of equal ...
๐ŸŒ
Matplotlib
matplotlib.org โ€บ stable โ€บ gallery โ€บ statistics โ€บ hist.html
Histograms โ€” Matplotlib 3.11.2 documentation
N_points = 100000 n_bins = 20 # Generate two normal distributions dist1 = rng.standard_normal(N_points) dist2 = 0.4 * rng.standard_normal(N_points) + 5 fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) # We can set the number of bins with the *bins* keyword argument.
Find elsewhere
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to adjust bin size in matplotlib histograms
How to Adjust Bin Size in Matplotlib Histograms
August 24, 2021 - The following code shows how to specify the number of bins to use in a histogram: import matplotlib.pyplot as 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 ...
๐ŸŒ
Canard Analytics
canardanalytics.com โ€บ blog โ€บ histogram-matplotlib
Plotting Histograms with Matplotlib | Canard Analytics
August 10, 2022 - Histogram bin sizing is set in the matplotlib.pyplot.hist function through the bins argument.
๐ŸŒ
Arab Psychology
scales.arabpsychology.com โ€บ home โ€บ how to easily customize histogram bin size in matplotlib
How To Easily Customize Histogram Bin Size In Matplotlib
December 4, 2025 - The following example illustrates how specifying bins=6 results in six uniform intervals, providing a balanced view of the small sample dataset. The code snippet below demonstrates how to define a small dataset and then generate a histogram ...
Top answer
1 of 4
87

The bins parameter tells you the number of bins that your data will be divided into. You can specify it as an integer or as a list of bin edges.

For example, here we ask for 20 bins:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(1000)
plt.hist(x, bins=20)

And here we ask for bin edges at the locations [-4, -3, -2... 3, 4].

plt.hist(x, bins=range(-4, 5))

Your question about how to choose the "best" number of bins is an interesting one, and there's actually a fairly vast literature on the subject. There are some commonly-used rules-of-thumb that have been proposed (e.g. the Freedman-Diaconis Rule, Sturges' Rule, Scott's Rule, the Square-root rule, etc.) each of which has its own strengths and weaknesses.

If you want a nice Python implementation of a variety of these auto-tuning histogram rules, you might check out the histogram functionality in the latest version of the AstroPy package, described here. This works just like plt.hist, but lets you use syntax like, e.g. hist(x, bins='freedman') for choosing bins via the Freedman-Diaconis rule mentioned above.

My personal favorite is "Bayesian Blocks" (bins="blocks"), which solves for optimal binning with unequal bin widths. You can read a bit more on that here.


Edit, April 2017: with matplotlib version 2.0 or later and numpy version 1.11 or later, you can now specify automatically-determined bins directly in matplotlib, by specifying, e.g. bins='auto'. This uses the maximum of the Sturges and Freedman-Diaconis bin choice. You can read more about the options in the numpy.histogram docs.

2 of 4
11

To complemented jakes answer, you can use numpy.histogram_bin_edges if you just want to calculate the optimal bin edges, without actually doing the histogram. histogram_bin_edges is a function specifically designed for the optimal calculation of bin edges. You can choose seven different algorithms for the optimisation.

๐ŸŒ
Pythoncharts
pythoncharts.com โ€บ matplotlib โ€บ histograms
Python Charts - Histograms in Matplotlib
December 6, 2020 - Here we show the bin values on the histogram. # Bins are: [, ) except for the last one which is [, ] n, bins, patches = plt.hist(df['total_bill'], align='mid', edgecolor='black') for num in bins: plt.text(num, 1, round(num, 1), ha='center', color='white') plt.gca().set_facecolor('#999') Let's load in the iris dataset and show an example of how to plot and compare histograms against each other with multiple variables or data.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ matplotlib โ€บ matplotlib_histogram.htm
Matplotlib - Histogram
In the following example, we are visualizing random data as a histogram with 30 bins, displaying it in green with a black edge. We are using the density=True parameter to represent the probability density โˆ’ ยท import matplotlib.pyplot as plt ...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:matplotlib โ€บ pyplot โ€บ .hist()
Python:Matplotlib | pyplot | .hist() | Codecademy
May 18, 2025 - You can specify the number of bins directly using the bins parameter. For example, plt.hist(data, bins=20) creates a histogram with 20 bins.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ data visualization โ€บ plotting-histogram-in-python-using-matplotlib
Plotting Histogram in Python using Matplotlib - GeeksforGeeks
Each histogram is plotted separately with 30 bins using different colors. Titles and axis labels are added to both plots, while tight_layout() automatically adjusts the spacing to prevent overlapping elements.
Published: July 16, 2026
๐ŸŒ
Matplotlib
matplotlib.org โ€บ 3.5.0 โ€บ api โ€บ _as_gen โ€บ matplotlib.pyplot.hist.html
matplotlib.pyplot.hist โ€” Matplotlib 3.5.0 documentation
March 26, 2021 - Location of the bottom of each bin, ie. bins are drawn from bottom to bottom + hist(x, bins) If a scalar, the bottom of each bin is shifted by the same amount. If an array, each bin is shifted independently and the length of bottom must match the number of bins. If None, defaults to 0. histtype{'bar', 'barstacked', 'step', 'stepfilled'}, default: 'bar' The type of histogram to draw.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.histogram.html
numpy.histogram โ€” NumPy v2.5 Manual
>>> a = np.arange(5) >>> hist, ... 2.4999999999999996 >>> np.sum(hist * np.diff(bin_edges)) 1.0 ยท Automated Bin Selection Methods example, using 2 peak random data with 2000 points. import matplotlib.pyplot as plt import numpy as np rng = np.random.RandomState(10) # deterministic ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 6986986 โ€บ bin-size-in-matplotlib-histogram
python - Bin size in Matplotlib (Histogram) - Stack Overflow
I'm using matplotlib to make a histogram. Is there any way to manually set the size of the bins as opposed to the number of bins?
๐ŸŒ
Matplotlib
matplotlib.org โ€บ 3.1.1 โ€บ gallery โ€บ statistics โ€บ hist.html
Histograms โ€” Matplotlib 3.1.2 documentation
N_points = 100000 n_bins = 20 # Generate a normal distribution, center at x=0 and y=5 x = np.random.randn(N_points) y = .4 * x + np.random.randn(100000) + 5 fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) # We can set the number of bins with the `bins` kwarg axs[0].hist(x, ...