Open In App

Matplotlib.pyplot.hist2d() in Python

Last Updated : 21 Apr, 2020
Suggest changes
Share
Like Article
Like
Report
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.hist2d() Function

The hist2d() function in pyplot module of matplotlib library is used to make a 2D histogram plot.
Syntax:matplotlib.pyplot.hist2d(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.pyplot.hist2d() function in matplotlib.pyplot: Example #1: Python3 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) + 50   plt.hist2d(x, y,  bins = 100,   norm = colors.LogNorm(),   cmap ="gray") plt.title('matplotlib.pyplot.hist2d() function \ Example\n\n', fontweight ="bold") plt.show() 
Output: Example #2: Python3 1==
#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=1000000),  multivariate_normal([30, 20],  [[2, 3], [1, 3]], size=100000) ]) plt.hist2d(result[:, 0],  result[:, 1],  bins = 100,   cmap = "Greens",  norm = colors.LogNorm()) plt.title('matplotlib.pyplot.hist2d function \ Example') plt.show() plt.hist2d(result[:, 0],   result[:, 1],  bins = 100,   cmap = "RdYlGn_r",  norm = colors.LogNorm()) plt.show() 
Output:

Next Article

Similar Reads

Article Tags :
Practice Tags :