Matplotlib.axes.Axes.hist2d() in Python (original) (raw)
Last Updated : 13 Apr, 2020
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:
Python3 `
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:
Python3 `
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: