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.

`pyplot`

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

`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**.

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)

It takes the following argument values.

and**x**

: These are the data values on the x-axis and y-axis. They must be of the same length.**y**

: These are the accumulated values in bins. Its default value is**C**`None`

.

: These are the number of hexagons in the x-axis or both axes. The default value of**gridsize**`gridsize`

is`100`

.

: This is used for the discretization of the hexagon color contrast values. Its default value is**bins**`None`

.

: This is the log**xscale**_{10}or linear scale along the horizontal axis. Its default value is`'linear'`

.

: This is the log**yscale**_{10}or linear scale along the vertical axis. Its default value is`'linear'`

.

: This is used to limit the number of bins to be plotted. Its default value is**extent**`'linear'`

.

: This is used to color bin values. It can be a string or colormap instance. Its default value is**cmap**`'linear'`

.

: These are additional keyword argument values.****kwargs**

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

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

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 plotimport datetimeimport numpy as np# seed a random numbernp.random.seed(int(datetime.datetime.utcnow().timestamp()))# total data pointsn = 200000x = np.random.standard_normal(n)y = 15 * np.random.standard_normal(n)# plot hexbin plot with grid size 70# in blue color shadesplot.hexbin(x, y, gridsize = 70, cmap ='Blues')plot.title('The hexbin() Example')# It is used to display results in the plot formatplot.savefig('output/graph.png')

- 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.

TRENDING TOPICS