Saturday, September 27, 2025
HomeLanguagesMatplotlib.axes.Axes.hist2d() in Python

Matplotlib.axes.Axes.hist2d() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.hist2d() Function

The Axes.hist2d() function in axes module of matplotlib library is used to make a 2D histogram plot.

Syntax: Axes.hist2d(self, x, y, bins=10, range=None, density=False, weights=None, cmin=None, cmax=None, *, data=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x, y : These parameter are the sequence of data.
  • bins : This parameter is an optional parameter and it contains the integer or sequence or string.
  • range : This parameter is an optional parameter and it the lower and upper range of the bins.
  • density : This parameter is an optional parameter and it contains the boolean values.
  • weights : This parameter is an optional parameter and it is an array of weights, of the same shape as x.
  • cmin : This parameter has all bins that has count less than cmin will not be displayed.
  • cmax : This parameter has all bins that has count more than cmax will not be displayed.

Returns: This returns the following:

  • h :This returns the bi-dimensional histogram of samples x and y.
  • xedges :This returns the bin edges along the x axis.
  • yedges :This returns the bin edges along the y axis.
  • image :This returns the QuadMesh.

Below examples illustrate the matplotlib.axes.Axes.hist2d() function in matplotlib.axes:

Example-1:




    
# Implementation of matplotlib function
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
import numpy as np
import matplotlib.pyplot as plt
  
N_points = 100000
x = np.random.randn(N_points)
y = .4 * x + np.random.randn(100000) + 5
  
fig, ax = plt.subplots()
ax.hist2d(x, y, bins = 100
          norm = colors.LogNorm(),
          cmap ="Greens")
  
ax.set_title('matplotlib.axes.Axes.\
hist2d() Example')
  
plt.show()


Output:

Example-2:




# Implementation of matplotlib function
from matplotlib import colors
import numpy as np
from numpy.random import multivariate_normal
import matplotlib.pyplot as plt
  
result = np.vstack([
    multivariate_normal([10, 10],
            [[3, 2], [2, 3]], size = 100000),
    multivariate_normal([30, 20],
            [[2, 3], [1, 3]], size = 1000)
])
  
fig, [axes, axes1] = plt.subplots(nrows = 2
                                  ncols = 1,
                                  sharex = True)
  
axes.hist2d(result[:, 0], result[:, 1],
            bins = 100, cmap ="GnBu",
            norm = colors.LogNorm())
  
axes1.hist2d(result[:, 0], result[:, 1],
             bins = 100, norm = colors.LogNorm())
  
axes.set_title('matplotlib.axes.Axes.\
hist2d() Example')
  
plt.show()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32322 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6804 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS