Python

Matplotlib 2d histogram

Matplotlib” is a commonly used data visualization library in Python, that enables us to create different types of graphs, charts, and plots. A histogram shows how data are distributed graphically. A “2D Histogram” is a type of histogram that visualizes the joint distribution of two continuous variables. It is used to explore the relationship between these two variables by dividing the data into a grid and assigning each grid cell a color based on the frequency of data points in that cell.

This post will illustrate the use of the “matplotlib.pyplot.hist2d()” function to create a “2D Histogram” in Python.

How to Plot a “Matplotlib 2-Dimension Histogram” in Python?

The “hist2d()” function of the “pyplot” module in the “matplotlib” library is used to plot a 2D histogram in Python.

Syntax

matplotlib.pyplot.hist2d(x, y, bins=10, range=None, density=False, weights=None, cmin=None, cmax=None, *, data=None, **kwargs)

Now, let’s plot a “2D histogram” using the below example code:

import matplotlib.pyplot as plt
import numpy
x = numpy.random.randn(1000)
y = numpy.random.randn(1000)
plt.hist2d(x, y, bins=30, cmap='Blues')
plt.colorbar()
plt.show()

In the above code:

  • The first step is to import the “matplotlib” and “numpy” libraries.
  • The “numpy.random.randn()” function is applied twice to generate random numbers within the specified range, i.e., “0-1000”.
  • The “plt.hist2d()” function is used to create a 2D histogram plot of “x” and “y” with 30 bins and a color map of “Blues”.
  • Lastly, the “plt.colorbar()” function adds the color bar, and the “plt.show()” function displays the graph.

Output

As seen, the “hist2d()” function creates a two-dimensional histogram with rectangular bins.

How to Customize a 2D Histogram in Matplotlib?

We can customize the plot by adding a title, labels to the axes, and changing the color map. An example code can be found here:

import matplotlib.pyplot as plt
import numpy
x = numpy.random.randn(1000)
y = numpy.random.randn(1000)
plt.hist2d(x, y, bins=30, cmap='jet')
plt.colorbar()
plt.title('2D Histogram')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

In the above lines of code:

  • The “hist2d()” function is used to create a 2D histogram of the two random number arrays “x” and “y” within the defined range, i.e., “0-1000”.
  • The “plt,title()”, “plt.xlabel()” and “plt.ylabel()” functions are used to add title and label the “x” and “y” axes, respectively.

Output

The above output implies that the “2D Histogram” is customized appropriately by setting the title and labeling the “x” and “y” axes.

Conclusion

In Python, the “plt.hist2d()” function of the “pyplot” module in the “matplotlib” library is used to plot a 2D histogram in Python. To customize the histogram, the “plt.title()”, “plt.xlabel()” and “plt.ylabel()” functions can be used by setting the title and labeling of the “x” and “y” axis, respectively. This post presented an in-depth guide on how to create a 2D histogram using the discussed function in the matplotlib library in Python.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.