To construct a histogram, the first step is to "bin" (or "bucket") the range of values—that is, divide the entire range of values into a series of intervals.

More information here: https://en.wikipedia.org/wiki/Histogram

Thus if you choose bins equal to 50 then your input will be divided into 50 intervals or bins if possible.

In matplotlib you can also let it automatically generate bins in the latest version given your data or you can give custom sequences as well.

More specific information about matplotlib for bins can be found here: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html

Answer from Udayan Tandon on Stack Overflow
🌐
Python Course
python-course.eu › numerical-programming › binning-in-python-and-pandas.php
34. Binning in Python and Pandas | Numerical Programming
February 3, 2025 - The following Python function can be used to create bins. def create_bins(lower_bound, width, quantity): """ create_bins returns an equal-width (distance) partitioning. It returns an ascending list of tuples, representing the intervals. A tuple bins[i], i.e.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › binning-data-in-python-with-scipy-numpy
Binning Data In Python With Scipy & Numpy - GeeksforGeeks
July 23, 2025 - Binning data is a common technique in data analysis where you group continuous data into discrete intervals, or bins, to gain insights into the distribution or trends within the data.
People also ask

What is Python binning?
Python binning is a data preprocessing technique used to group a set of continuous values into a smaller number of "bins". It can help improve accuracy in predictive models, especially when dealing with overfitting.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
What are the benefits of binning in Python?
Binning in Python can help reduce noise, transform continuous variables into categorical counterparts, and improve the performance of machine learning models.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
What are the different techniques for binning data in Python?
The most common techniques for binning data in Python include equal-width binning, equal-frequency binning, and k-means clustering. Python libraries like NumPy and Pandas provide functions to implement these techniques.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
🌐
W3Schools
w3schools.com › python › ref_func_bin.asp
Python bin() Function
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The bin() function returns the binary version of a specified integer....
🌐
Programiz
programiz.com › python-programming › methods › built-in › bin
Python bin() (With Examples)
The bin() method converts a specified integer number to its binary representation and returns it. In this tutorial, you will learn about the Python bin() method with the help of examples.
🌐
Train in Data
blog.trainindata.com › master-data-binning-in-python-using-pandas
Master Data Binning in Python using Pandas | Train in Data Blog
February 23, 2023 - In this tutorial, we’ll look into binning data in Python using the cut and qcut functions from the open-source library pandas.
🌐
GeeksforGeeks
geeksforgeeks.org › python › bin-in-python
bin() in Python - GeeksforGeeks
November 15, 2023 - Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 04.05-histograms-and-binnings.html
Histograms, Binnings, and Density | Python Data Science Handbook
For this purpose, Matplotlib provides the plt.hexbin routine, which will represents a two-dimensional dataset binned within a grid of hexagons: ... plt.hexbin has a number of interesting options, including the ability to specify weights for each point, and to change the output in each bin to any NumPy aggregate (mean of weights, standard deviation of weights, etc.).
Find elsewhere
🌐
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 count) of data points that fall within specific ranges, providing a visual sense of how data is distributed. In Python, 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.
🌐
Kanaries
docs.kanaries.net › topics › Python › python-binning
Python Binning: Clearly Explained – Kanaries
August 17, 2023 - PyGWalker (opens in a new tab) ... User Interface for visual exploration. ... Python binning is a data preprocessing technique used to group a set of continuous values into a smaller number of "bins"....
🌐
Statology
statology.org › home › how to perform data binning in python (with examples)
How to Perform Data Binning in Python (With Examples)
December 14, 2021 - #perform data binning on points variable df['points_bin'] = pd.qcut(df['points'], q=3) #view updated DataFrame print(df) points assists rebounds points_bin 0 4 2 7 (3.999, 10.667] 1 4 5 7 (3.999, 10.667] 2 7 4 4 (3.999, 10.667] 3 8 7 6 (3.999, 10.667] 4 12 7 3 (10.667, 19.333] 5 13 8 8 (10.667, 19.333] 6 15 5 9 (10.667, 19.333] 7 18 4 9 (10.667, 19.333] 8 22 5 12 (19.333, 25.0] 9 23 11 11 (19.333, 25.0] 10 23 13 8 (19.333, 25.0] 11 25 8 9 (19.333, 25.0] Notice that each row of the data frame has been placed in one of three bins based on the value in the points column.
🌐
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 ...
🌐
Practical Business Python
pbpython.com › pandas-qcut-cut.html
Binning Data with Pandas qcut and cut - Practical Business Python
When dealing with continuous numeric data, it is often helpful to bin the data into multiple buckets for further analysis. There are several different terms for binning including bucketing, discrete binning, discretization or quantization. Pandas supports these approaches using the cut and qcut functions.
🌐
Medium
medium.com › @kelvinsang97 › binning-bucketing-discretization-in-python-be665936a090
Binning/Bucketing/Discretization in Python | by Kelvin Kipsang | Medium
January 3, 2023 - For example, if binning an ‘Loan_amount’ column, we know Group 1 are between 0 and 1000 , 1000–10000 are Group 2, 10000–10000 are Group 3. So we can appropriately set bins=[0 ,1000,10000,20000] and labels=[‘Group 1’, ‘Group 2’, ‘Group 3’]. In qcut, when we specify q=3, we are telling pandas to cut the Loan_amount column into 3 equal quantiles, i.e.0–33.3%, 33.3%–66.6%, 66.6–99.99% buckets/bins.
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to bin numerical data with pandas
How to Bin Numerical Data with Pandas | Towards Data Science
January 28, 2025 - The task is to bin the numerical scores into grades of values "A", "B" and "C" where "A" is the best grade and "C" is the worse grade. ... Pandas .between method returns a boolean vector containing True wherever the corresponding Series element is between the boundary values left and right[1]. ... Where square brackets [ and round brackets )indicates that the boundary value is inclusive and exclusive respectively.
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.

🌐
CodeRivers
coderivers.org › blog › bins-python
Understanding and Using Bins in Python - CodeRivers
February 22, 2026 - In Python, the concept of "bins" often arises in various data analysis, statistical, and data visualization tasks. Bins are used to group data into intervals or categories. This process of binning data can be extremely useful for summarizing large datasets, creating histograms, and performing ...
🌐
CodeSignal
codesignal.com › learn › courses › data-cleaning-and-preprocessing-techniques › lessons › data-binning-techniques-an-introduction-and-implementation-with-python-and-pandas
An Introduction and Implementation with Python and Pandas
In the example above, we utilized the pd.cut() function to divide a set of ages into distinct age groups or bins. This approach allows us to categorize a wide range of ages into a selected age group, simplifying data analysis.
🌐
Medium
medium.com › @tubelwj › pandas-data-preprocessing-data-binning-40614ab13b54
Pandas Data Preprocessing — Data Binning | by Gen. Devin DL. | Medium
December 12, 2023 - In this case, we define the edges of each bin. In Python, binning by distance in pandas can be achieved using the cut() function.