matplotlib.axes.Axes.hexbin — Matplotlib 3.10.1 documentation (original) (raw)

Axes.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', reduce_C_function=, mincnt=None, marginals=False, colorizer=None, data=None, **kwargs)[source]#

Make a 2D hexagonal binning plot of points x, y.

If C is None, the value of the hexagon is determined by the number of points in the hexagon. Otherwise, C specifies values at the coordinate (x[i], y[i]). For each hexagon, these values are reduced using reduce_C_function.

Parameters:

x, yarray-like

The data positions. x and y must be of the same length.

Carray-like, optional

If given, these values are accumulated in the bins. Otherwise, every point has a value of 1. Must be of the same length as _x_and y.

gridsizeint or (int, int), default: 100

If a single int, the number of hexagons in the _x_-direction. The number of hexagons in the _y_-direction is chosen such that the hexagons are approximately regular.

Alternatively, if a tuple (nx, ny), the number of hexagons in the _x_-direction and the _y_-direction. In the_y_-direction, counting is done along vertically aligned hexagons, not along the zig-zag chains of hexagons; see the following illustration.

(Source code, 2x.png, png)

To get approximately regular hexagons, choose\(n_x = \sqrt{3}\,n_y\).

bins'log' or int or sequence, default: None

Discretization of the hexagon values.

xscale{'linear', 'log'}, default: 'linear'

Use a linear or log10 scale on the horizontal axis.

yscale{'linear', 'log'}, default: 'linear'

Use a linear or log10 scale on the vertical axis.

mincntint >= 0, default: None

If not None, only display cells with at least _mincnt_number of points in the cell.

marginalsbool, default: False

If marginals is True, plot the marginal density as colormapped rectangles along the bottom of the x-axis and left of the y-axis.

extent4-tuple of float, default: None

The limits of the bins (xmin, xmax, ymin, ymax). The default assigns the limits based on_gridsize_, x, y, xscale and yscale.

If xscale or yscale is set to 'log', the limits are expected to be the exponent for a power of 10. E.g. for x-limits of 1 and 50 in 'linear' scale and y-limits of 10 and 1000 in 'log' scale, enter (1, 50, 1, 3).

Returns:

PolyCollection

A PolyCollection defining the hexagonal bins.

If marginals is True, horizontal bar and vertical bar (both PolyCollections) will be attached to the return collection as attributes hbar and vbar.

Other Parameters:

cmapstr or Colormap, default: [rcParams["image.cmap"]](../../users/explain/customizing.html?highlight=image.cmap#matplotlibrc-sample) (default: 'viridis')

The Colormap instance or registered colormap name used to map scalar data to colors.

normstr or Normalize, optional

The normalization method used to scale scalar data to the [0, 1] range before mapping to colors using cmap. By default, a linear scaling is used, mapping the lowest value to 0 and the highest to 1.

If given, this can be one of the following:

vmin, vmaxfloat, optional

When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. It is an error to use_vmin_/vmax when a norm instance is given (but using a str _norm_name together with vmin/vmax is acceptable).

alphafloat between 0 and 1, optional

The alpha blending value, between 0 (transparent) and 1 (opaque).

linewidthsfloat, default: None

If None, defaults to [rcParams["patch.linewidth"]](../../users/explain/customizing.html?highlight=patch.linewidth#matplotlibrc-sample) (default: 1.0).

edgecolors{'face', 'none', None} or color, default: 'face'

The color of the hexagon edges. Possible values are:

reduce_C_functioncallable, default: numpy.mean

The function to aggregate C within the bins. It is ignored if_C_ is not given. This must have the signature:

def reduce_C_function(C: array) -> float

Commonly used functions are:

By default will only reduce cells with at least 1 point because some reduction functions (such as numpy.amax) will error/warn with empty input. Changing mincnt will adjust the cutoff, and if set to 0 will pass empty input to the reduction function.

colorizerColorizer or None, default: None

The Colorizer object used to map color to data. If None, a Colorizer object is created from a norm and cmap.

dataindexable object, optional

If given, the following parameters also accept a string s, which is interpreted as data[s] if s is a key in data:

x, y, C

**kwargsPolyCollection properties

All other keyword arguments are passed on to PolyCollection:

See also

hist2d

2D histogram rectangular bins

Examples using matplotlib.axes.Axes.hexbin#