What is the matplotlib.pyplot.hexbin() in Python?

The Matplotlib library in Python helps us fabricate animated and interactive data visualization. It is also used to perform many statistical functions, including the calculation of average, median, and variance, and plot data inferences.

Hexbin in Matplotlib

What is pyplot?

The pyplot interface helps Matplotlib to visualize data in the form of graphs and shapes.

What is the hexbin() function?

The hexbin() function in the pyplot library is used to represent data in the 2D hexagonal plot by binning the points from the x-axis and y-axis.

This function is mostly used when data has a lot of points. To avoid overlapping, the plot window is divided into numerous hex bins. Each hex bin color represents a fixed number of points in it. The size of hex bins can be changed using the gridsize parameter.

Syntax

matplotlib.pyplot.hexbin(x, y, C=None, gridsize=100, bins=None,
xscale='linear', yscale='linear', extent=None, cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors='face',
mincnt=None, marginals=False, *, data=None, **kwargs)

Parameters

It takes the following argument values.

  • x and y: These are the data values on the x-axis and y-axis. They must be of the same length.
  • C: These are the accumulated values in bins. Its default value is None.
  • gridsize: These are the number of hexagons in the x-axis or both axes. The default value of gridsize is 100.
  • bins: This is used for the discretization of the hexagon color contrast values. Its default value is None.
  • xscale: This is the log10 or linear scale along the horizontal axis. Its default value is 'linear'.
  • yscale: This is the log10 or linear scale along the vertical axis. Its default value is 'linear'.
  • extent: This is used to limit the number of bins to be plotted. Its default value is 'linear'.
  • cmap: This is used to color bin values. It can be a string or colormap instance. Its default value is 'linear'.
  • **kwargs: These are additional keyword argument values.

Note: The data values should be of the same length on both the x and y axes.

Return value

The result-driven values will be presented as a polycollection representing the hexagonal bins.

Example

In this code snippet, we are going to discuss the hexbin plot. It is used when we have a lot of data, and we want to plot two numeric points.

import matplotlib.pyplot as plot
import datetime
import numpy as np
# seed a random number
np.random.seed(int(datetime.datetime.utcnow().timestamp()))
# total data points
n = 200000
x = np.random.standard_normal(n)
y = 15 * np.random.standard_normal(n)
# plot hexbin plot with grid size 70
# in blue color shades
plot.hexbin(x, y, gridsize = 70, cmap ='Blues')
plot.title('The hexbin() Example')
# It is used to display results in the plot format
plot.savefig('output/graph.png')

Explanation

  • Lines 1–3: We import the matplotlib.pyplot, datatime, and numPy modules.
  • Line 5: The np.random.seed() function returns a random number, taking timestamp as input seed.
  • Lines 7–9: We create data samples of standard normal distribution.
  • Line 12: We invoke plot.hexbin() to generate the plot on the above-created data points.
  • Line 13: We set the title of the hexbin plot generated.

Free Resources