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.
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.
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')
matplotlib.pyplot
, datatime
, and numPy
modules.np.random.seed()
function returns a random number, taking timestamp as input seed.plot.hexbin()
to generate the plot on the above-created data points.