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 › 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)
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 › 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.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)
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)
NumPy
numpy.org › doc › 1.18 › reference › generated › numpy.histogram2d.html
numpy.histogram2d — NumPy v1.18 Manual
May 24, 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: >>> ...
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.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))
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.
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))
Top answer 1 of 4
30
If you have the raw data from the counts, you could use plt.hexbin to create the plots for you (IMHO this is better than a square lattice): Adapted from the example of hexbin:
import numpy as np
import matplotlib.pyplot as plt
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
plt.hexbin(x,y)
plt.show()

If you already have the Z-values in a matrix as you mention, just use plt.imshow or plt.matshow:
XB = np.linspace(-1,1,20)
YB = np.linspace(-1,1,20)
X,Y = np.meshgrid(XB,YB)
Z = np.exp(-(X**2+Y**2))
plt.imshow(Z,interpolation='none')

2 of 4
16
If you have not only the 2D histogram matrix but also the underlying (x, y) data, then you could make a scatter plot of the (x, y) points and color each point according to its binned count value in the 2D-histogram matrix:
import numpy as np
import matplotlib.pyplot as plt
n = 10000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
xedges, yedges = np.linspace(-4, 4, 42), np.linspace(-25, 25, 42)
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
xidx = np.clip(np.digitize(x, xedges), 0, hist.shape[0]-1)
yidx = np.clip(np.digitize(y, yedges), 0, hist.shape[1]-1)
c = hist[xidx, yidx]
plt.scatter(x, y, c=c)
plt.show()

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.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,))
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 04.05-histograms-and-binnings.html
Histograms, Binnings, and Density | Python Data Science Handbook
Just as with plt.hist, plt.hist2d has a number of extra options to fine-tune the plot and the binning, which are nicely outlined in the function docstring. Further, just as plt.hist has a counterpart in np.histogram, plt.hist2d has a counterpart in np.histogram2d, which can be used as follows:
NumPy
numpy.org › doc › stable › reference › generated › numpy.histogramdd.html
numpy.histogramdd — NumPy v2.4 Manual
January 31, 2021 - histogram2d · 2-D histogram · Examples · Try it in your browser!

