Suppose you have a 1D array, you plot the position of its values on the x axis, they are so dense that you can't tell the spatial distribution, you use a 1D histogram to show the distribution by count of boxes along the x axis. Problem solved.

Then you have two 1D arrays, a list of 2D dots in (x, y) axes. You plot their positions on the x-y plane, again they are so dense and overlap with each other. You want to view the distribution better by count of boxes in the plane, so you try a 2D diagram. Problem solved.

Here is an example

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

# prepare 2D random dots centered at (0, 0)
n = 100000
x = np.random.randn(n)
y = x + np.random.randn(n)

# plot data
fig1 = plt.figure()
plt.plot(x,y,'.r')
plt.xlabel('x')
plt.ylabel('y')

gives

# plot 2D histogram using pcolor
fig2 = plt.figure()
plt.hist2d(x, y, bins=100)
plt.xlabel('x')
plt.ylabel('y')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')

gives

Answer from Neo X on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.5 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.0 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.6.dev0 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.1 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.10 Manual
>>> x = np.random.normal(3, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.2 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.13 Manual
>>> x = np.random.normal(2, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) >>> H = H.T # Let each row list bins with common y range.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.4 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v2.3 Manual
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.9 Manual
October 18, 2015 - >>> x = np.random.normal(3, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))
🌐
NumPy
numpy.org › doc › 1.17 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.17 Manual
February 18, 2020 - Examples · >>> from matplotlib.image import NonUniformImage >>> import matplotlib.pyplot as plt · Construct a 2-D histogram with variable bin width. First define the bin edges: >>> xedges = [0, 1, 3, 5] >>> yedges = [0, 2, 3, 4, 6] Next we create a histogram H with random bin content: >>> ...
🌐
SciPy
docs.scipy.org › doc › numpy-1.15.0 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.15 Manual - SciPy.org
>>> x = np.random.normal(2, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) >>> H = H.T # Let each row list bins with common y range.
🌐
SciPy
docs.scipy.org › doc › numpy-1.14.1 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.14 Manual
April 16, 2018 - >>> x = np.random.normal(2, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) >>> H = H.T # Let each row list bins with common y range.
🌐
Python Pool
pythonpool.com › home › numpy › numpy histogram(): bins, counts, and density
Numpy histogram() Function With Plotting and Examples
July 13, 2026 - x = np.array([0.1, 0.4, 1.2, 1.8]) y = np.array([1.0, 1.5, 0.7, 1.8]) H, xedges, yedges = np.histogram2d(x, y, bins=2) When visualizing a 2D histogram, remember that NumPy stores x along the first array dimension and y along the second dimension.
🌐
CodeSpeedy
codespeedy.com › home › how does numpy.histogram2d works in python
How does numpy.histogram2d works in Python - CodeSpeedy
October 28, 2022 - x = np.random.normal(2, 1, 100) y = np.random.normal(1, 1, 100) H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) H = H.T
🌐
Python Pool
pythonpool.com › home › matplotlib › matplotlib 2d histogram: hist2d(), bins, lognorm, and colorbars
Matplotlib 2D Histogram: hist2d(), Bins, LogNorm, and Colorbars
July 13, 2026 - import numpy as np rng = np.random.default_rng(11) x = rng.normal(size=1000) y = rng.normal(size=1000) counts, xedges, yedges = np.histogram2d(x, y, bins=20) print(counts.shape) print(xedges[:3]) print(yedges[:3]) For summary statistics around ...
🌐
SciPy
docs.scipy.org › doc › numpy-1.6.0 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.6 Manual (DRAFT)
May 15, 2011 - >>> x, y = np.random.randn(2, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(5, 8)) >>> H.shape, xedges.shape, yedges.shape ((5, 8), (6,), (9,))
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-create-a-2d-histogram-with-matplotlib-
How to create a 2d histogram with matplotlib ? - Moonbooks
May 14, 2019 - heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) #extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] plt.imshow(heatmap,origin='lower') #plt.imshow(heatmap,origin='lower', extent=extent) plt.title("How to plot a 2d histogram with matplotlib ?") plt.savefig("histogram_2d_07.png", ...