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.

Answer from jakevdp on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ bin-size-in-matplotlib-histogram
Bin Size in Matplotlib Histogram - GeeksforGeeks
July 23, 2025 - DSA Python ยท Data Science ยท NumPy ... 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....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.histogram.html
numpy.histogram โ€” NumPy v2.5 Manual
Input data. The histogram is computed over the flattened array. ... If bins is an int, it defines the number of equal-width bins in the given range (10, by default).
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.

๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 04.05-histograms-and-binnings.html
Histograms, Binnings, and Density | Python Data Science Handbook
The two-dimensional histogram creates a tesselation of squares across the axes. Another natural shape for such a tesselation is the regular hexagon. For this purpose, Matplotlib provides the plt.hexbin routine, which will represents a two-dimensional dataset binned within a grid of hexagons:
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ data-visualization-matplotlib-seaborn โ€บ chapter-3-basic-matplotlib-plot-types โ€บ matplotlib-histogram-bins
Understanding Histogram Bins in Matplotlib
They display the frequency (or ... plt.hist() is commonly used to create these visualizations. The bars in a histogram represent these frequencies, with each bar corresponding to a specific range of values. These ranges are called bins....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.histogram.html
numpy.histogram โ€” NumPy v2.1 Manual
Input data. The histogram is computed over the flattened array. ... If bins is an int, it defines the number of equal-width bins in the given range (10, by default).
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Astropy
docs.astropy.org โ€บ en โ€บ stable โ€บ visualization โ€บ histogram.html
Choosing Histogram Bins โ€” Astropy v8.0.1
The simplest methods of tuning the number of bins are the normal reference rules due to Scott (implemented in scott_bin_width()) and Freedman & Diaconis (implemented in freedman_bin_width()). These rules proceed by assuming the data is close to normally-distributed, and applying a rule-of-thumb intended to minimize the difference between the histogram and the underlying distribution of data.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ histograms-matplotlib
Histograms in Matplotlib | DataCamp
June 17, 2019 - Apart, from numerical data, Histograms ... of picture elements (pixels) ranging from $0$ to $255$. The x-axis of the histogram denotes the number of bins while the y-axis represents the frequency of a particular bin....
๐ŸŒ
Canard Analytics
canardanalytics.com โ€บ blog โ€บ histogram-matplotlib
Plotting Histograms with Matplotlib | Canard Analytics
August 10, 2022 - Once the data is classified, the frequency of data in each bin is determined by adding all entries in a given bin, and the resulting distribution is plotted with the bin intervals on the x-axis and the number of entries of each bin on the y-axis. Matplotlib has a powerful histogram functionality built into the package which is accessed through the matplotlib.pyplot.hist function.
๐ŸŒ
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:
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-statistics-with-python โ€บ modules โ€บ histograms โ€บ cheatsheet
Learn Statistics with Python: Histograms Cheatsheet | Codecademy
In Python, the pyplot.hist() function in the Matplotlib pyplot library can be used to plot a histogram. The function accepts a NumPy array, the range of the dataset, and the number of bins as input.
๐ŸŒ
CK-12 Foundation
ck12.org โ€บ all subjects โ€บ math grade 6 โ€บ frequency tables and histograms โ€บ what are bins in a histogram in python?
Flexi answers - What are bins in a histogram in Python? | CK-12 Foundation
September 11, 2025 - In a histogram, bins are the intervals or ranges into which data points are grouped. Each bin represents a specific range of values, and the height of the bar for that bin shows how many data points fall within that range. Here's a quick breakdown: Purpose: Bins help visualize the distribution ...
๐ŸŒ
Plotly
plotly.com โ€บ python โ€บ histograms
Histograms in Python
With the histnorm argument, it is also possible to represent the percentage or fraction of samples in each bin (histnorm='percent' or probability), or a density histogram (the sum of all bar areas equals the total number of sample points, density), or a probability density histogram (the sum of all bar areas equals 1, probability density).
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.histogram.html
numpy.histogram โ€” NumPy v2.2 Manual
Input data. The histogram is computed over the flattened array. ... If bins is an int, it defines the number of equal-width bins in the given range (10, by default).
๐ŸŒ
Your Data Teacher
yourdatateacher.com โ€บ home โ€บ how to choose the bins of a histogram?
How to choose the bins of a histogram? | Your Data Teacher
November 22, 2021 - If we plot the histogram using plt.hist function, the default number of bins is 10. ... As we can see, itโ€™s similar to the original normal distribution, but itโ€™s still a bit coarse-grained. If we increase the number of bins to 100 we get:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to add bin labels to histogram in matplotlib?
r/learnpython on Reddit: How to add bin labels to histogram in matplotlib?
April 2, 2021 -

Hello,

So I created a histogram using matplotlib and seaborn:

sns.set()
_ = plt.hist(df['value'], bins=[1,25,45,55,80])
_ = plt.xlabel('value')
_ = plt.ylabel('frequency')
_ = plt.title('histogram title')
plt.show()

and I am given a histogram, but the x-axis marks do not reflect my histogram bin ranges. I recognize I have uneven bin ranges, but these ranges are necessary for my research question. Instead my x-axis just shows intervals of 20, which would not allow the viewer to discern what the exact bin ranges are, though the bars of the histogram are of different widths reflecting the different bin ranges.

My question is, how do I augment my python code to show:

  1. my bin ranges, so that on the x-axis, or pointing to the bars themselves, I can see '1-25', '25-45', etc.

  2. the actual frequency value of each bar

Thanks!

EDIT: I realized I probably just need to make a legend...

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ data visualization โ€บ plotting-histogram-in-python-using-matplotlib
Plotting Histogram in Python using Matplotlib - GeeksforGeeks
The histplot() function creates a histogram with 30 bins, while kde=True overlays a smooth density curve that represents the data distribution.
Published: July 16, 2026
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ statistics-histograms โ€บ modules โ€บ stats-histograms-course โ€บ cheatsheet
Statistics: Histograms: Histograms Cheatsheet | Codecademy
Learn how to work with bins and breaks to describe the distribution of a dataset. ... In Python, the pyplot.hist() function in the Matplotlib pyplot library can be used to plot a histogram.