🌐
GeeksforGeeks
geeksforgeeks.org › python › bin-size-in-matplotlib-histogram
Bin Size in Matplotlib Histogram - GeeksforGeeks
July 23, 2025 - 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.
🌐
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 bin width in a histogram: import matplotlib.pyplot as 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))
🌐
Matplotlib
matplotlib.org › stable › api › _as_gen › matplotlib.pyplot.hist.html
matplotlib.pyplot.hist — Matplotlib 3.11.2 documentation
If 'horizontal', barh will be used for bar-type histograms and the bottom kwarg will be the left edges. ... The relative width of the bars as a fraction of the bin width.
🌐
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 - For instance, setting bins=10 instructs Matplotlib to divide the total data range into ten segments, with the width of each segment being calculated automatically as (max(data) - min(data)) / 10.
🌐
Delft Stack
delftstack.com › home › howto › matplotlib › how to manually set the size of the bins in matplotlib
How to Manually Set the Size of the Bins in Matplotlib Histogram | Delft Stack
February 2, 2024 - To manually set the size of the bins in Matplotlib we calculate the number of bins for required width and pass no. of bins as a parameter in hist2d() function.
🌐
Astropy
docs.astropy.org › en › stable › visualization › histogram.html
Choosing Histogram Bins — Astropy v8.0.1
Knuth’s rule chooses a constant bin size which minimizes the error of the histogram’s approximation to the data, while the Bayesian Blocks uses a more flexible method which allows varying bin widths. Because both of these require the minimization of a cost function across the dataset, they are more computationally intensive than the rules-of-thumb mentioned above. Here are the results of these procedures for the above dataset: import warnings import numpy as np import matplotlib.pyplot as plt from astropy.visualization import hist # generate some complicated data rng = np.random.default_rn
🌐
TutorialsPoint
tutorialspoint.com › how-to-make-two-histograms-have-the-same-bin-width-in-matplotlib
How to make two histograms have the same bin width in Matplotlib?
May 11, 2021 - To make two histograms having same bin width, we can compute the histogram of a set of data. Create random data, a, and normal distribution, b. Initialize a variable, bins, for the same bin width. Plot a and bins using hist() method. Plot b and bins using hist() method.
Find elsewhere
🌐
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 width spanning the range of your data...
Top answer
1 of 3
5

I had a similar question here, and the answer was to use a dirty hack. Matplotlib histogram with collection bin for high values

So with the following code, you get the ugly histogram you already have.

def plot_histogram_04():
    limit1, limit2 = 50, 550
    binwidth1, binwidth2 = 10, 50    
    data = np.hstack((np.random.rand(1000) * limit1, np.random.rand(100) * limit2))

    bins = range(0, limit1, binwidth1) + range(limit1, limit2, binwidth2)

    plt.subplots(1, 1)
    plt.hist(data, bins=bins)
    plt.savefig('my_plot_04.png')
    plt.close()

In order to make the bins equal width, you indeed have to make them equal width! This means manipulating your data such that they all fall in bins with equal width, and then play around with the xlabel.

def plot_histogram_05():
    limit1, limit2 = 50, 550
    binwidth1, binwidth2 = 10, 50

    data = np.hstack((np.random.rand(1000) * limit1, np.random.rand(100) * limit2))

    orig_bins = range(0, limit1, binwidth1) + range(limit1, limit2 + binwidth2, binwidth2)
    data = [(i - limit1) / (binwidth2 / binwidth1) + limit1 
            if i >= limit1 else i for i in data]
    bins = range(0, limit2 / (binwidth2 / binwidth1) + limit1, binwidth1)

    _, ax = plt.subplots(1, 1)
    plt.hist(data, bins=bins)

    xlabels = np.array(orig_bins, dtype='|S3')
    N_labels = len(xlabels)
    print xlabels
    print bins
    plt.xlim([0, bins[-1]])
    plt.xticks(binwidth1 * np.arange(N_labels))
    ax.set_xticklabels(xlabels)

    plt.savefig('my_plot_05.png')
    plt.close()

2 of 3
2

You can use bar and there is no need to split the axis. Here is an example,

import matplotlib.pylab as plt
import numpy as np

data = np.hstack((np.random.rand(1000)*50,np.random.rand(100)*500))
binwidth1,binwidth2=10,50
bins=range(0,50,binwidth1)+range(50,550,binwidth2)

fig,(ax) = plt.subplots(1, 1)

y,binEdges=np.histogram(data,bins=bins)

ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[:5], y[:5],width=.8*binwidth1,align='center')
ax.bar(0.5*(binEdges[1:]+binEdges[:-1])[5:], y[5:],width=.8*binwidth1,align='center')
plt.show()

In case you really want to split the axis have a look here.

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.histogram.html
numpy.histogram — NumPy v2.5 Manual
If bins is a string, it defines the method used to calculate the optimal bin width, as defined by histogram_bin_edges.
🌐
Matplotlib
matplotlib.org › 3.1.1 › api › _as_gen › matplotlib.pyplot.hist.html
matplotlib.pyplot.hist — Matplotlib 3.1.2 documentation
matplotlib.pyplot.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)[source]¶ · Plot a histogram.
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › histograms-matplotlib
Histograms in Matplotlib | DataCamp
June 17, 2019 - Next, let's plot the histogram using matplotlib's plt.bar function where your x-axis and y-axis will be bin_edges and hist, respectively. import matplotlib.pyplot as plt %matplotlib inline · plt.figure(figsize=[10,8]) plt.bar(bin_edges[:-1], hist, width = 0.5, color='#0504aa',alpha=0.7) ...
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 04.05-histograms-and-binnings.html
Histograms, Binnings, and Density | Python Data Science Handbook
For the generalization of this histogram binning in dimensions higher than two, see the np.histogramdd function. 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:
🌐
Matplotlib
matplotlib.org › 3.5.0 › api › _as_gen › matplotlib.pyplot.hist.html
matplotlib.pyplot.hist — Matplotlib 3.5.0 documentation
March 26, 2021 - If 'horizontal', barh will be used for bar-type histograms and the bottom kwarg will be the left edges. ... The relative width of the bars as a fraction of the bin width.
🌐
Matplotlib
matplotlib.org › stable › gallery › statistics › histogram_histtypes.html
Demo of the histogram function's different histtype settings — Matplotlib 3.11.2 documentation
Histogram with custom and unequal bin widths. Two histograms with stacked bars. Selecting different bin counts and sizes can significantly affect the shape of a histogram. The Astropy docs have a great section on how to select these parameters: http://docs.astropy.org/en/stable/visualization/histogram.html · import matplotlib.pyplot as plt import numpy as np np.random.seed(19680801) mu_x = 200 sigma_x = 25 x = np.random.normal(mu_x, sigma_x, size=100) mu_w = 200 sigma_w = 10 w = np.random.normal(mu_w, sigma_w, size=100) fig, axs = plt.subplots(nrows=2, ncols=2) axs[0, 0].hist(x, 20, density=T
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.histogram.html
numpy.histogram — NumPy v2.2 Manual
If bins is a string, it defines the method used to calculate the optimal bin width, as defined by histogram_bin_edges.
🌐
Matplotlib
matplotlib.org › 3.3.4 › api › _as_gen › matplotlib.pyplot.hist.html
matplotlib.pyplot.hist — Matplotlib 3.3.4 documentation
January 28, 2021 - matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)[source]¶ · Plot a histogram.