matplotlib

Travis-CI:

This Page

matplotlib.axes.Axes.hexbin

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='none', reduce_C_function=<function mean>, mincnt=None, marginals=False, **kwargs)

Make a hexagonal binning plot.

Call signature:

hexbin(x, y, C = None, gridsize = 100, bins = None,
       xscale = 'linear', yscale = 'linear',
       cmap=None, norm=None, vmin=None, vmax=None,
       alpha=None, linewidths=None, edgecolors='none'
       reduce_C_function = np.mean, mincnt=None, marginals=True
       **kwargs)

Make a hexagonal binning plot of x versus y, where x, y are 1-D sequences of the same length, N. If C is None (the default), this is a histogram of the number of occurences of the observations at (x[i],y[i]).

If C is specified, it specifies values at the coordinate (x[i],y[i]). These values are accumulated for each hexagonal bin and then reduced according to reduce_C_function, which defaults to numpy’s mean function (np.mean). (If C is specified, it must also be a 1-D sequence of the same length as x and y.)

x, y and/or C may be masked arrays, in which case only unmasked points will be plotted.

Optional keyword arguments:

gridsize: [ 100 | integer ]
The number of hexagons in the x-direction, default is 100. The corresponding number of hexagons in the y-direction is chosen such that the hexagons are approximately regular. Alternatively, gridsize can be a tuple with two elements specifying the number of hexagons in the x-direction and the y-direction.
bins: [ None | ‘log’ | integer | sequence ]

If None, no binning is applied; the color of each hexagon directly corresponds to its count value.

If ‘log’, use a logarithmic scale for the color map. Internally, is used to determine the hexagon color.

If an integer, divide the counts in the specified number of bins, and color the hexagons accordingly.

If a sequence of values, the values of the lower bound of the bins to be used.

xscale: [ ‘linear’ | ‘log’ ]
Use a linear or log10 scale on the horizontal axis.
yscale: [ ‘linear’ | ‘log’ ]
Use a linear or log10 scale on the vertical axis.
mincnt: [ None | a positive integer ]
If not None, only display cells with more than mincnt number of points in the cell
marginals: [ True | False ]
if marginals is True, plot the marginal density as colormapped rectagles along the bottom of the x-axis and left of the y-axis
extent: [ None | scalars (left, right, bottom, top) ]

The limits of the bins. 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).

Other keyword arguments controlling color mapping and normalization arguments:

cmap: [ None | Colormap ]
a matplotlib.colors.Colormap instance. If None, defaults to rc image.cmap.
norm: [ None | Normalize ]
matplotlib.colors.Normalize instance is used to scale luminance data to 0,1.
vmin / vmax: scalar
vmin and vmax are used in conjunction with norm to normalize luminance data. If either are None, the min and max of the color array C is used. Note if you pass a norm instance, your settings for vmin and vmax will be ignored.
alpha: scalar between 0 and 1, or None
the alpha value for the patches
linewidths: [ None | scalar ]
If None, defaults to 1.0. Note that this is a tuple, and if you set the linewidths argument you must set it as a sequence of floats, as required by RegularPolyCollection.

Other keyword arguments controlling the Collection properties:

edgecolors: [ None | 'none' | mpl color | color sequence ]

If 'none', draws the edges in the same color as the fill color. This is the default, as it avoids unsightly unpainted pixels between the hexagons.

If None, draws the outlines in the default color.

If a matplotlib color arg or sequence of rgba tuples, draws the outlines in the specified color.

Here are the standard descriptions of all the Collection kwargs:

Property Description
agg_filter unknown
alpha float or None
animated [True | False]
antialiased or antialiaseds Boolean or sequence of booleans
array unknown
axes an Axes instance
bottom_margin unknown
clim a length 2 sequence of floats
clip_box a matplotlib.transforms.Bbox instance
clip_on [True | False]
clip_path [ (Path, Transform) | Patch | None ]
cmap a colormap or registered colormap name
color matplotlib color arg or sequence of rgba tuples
contains a callable function
edgecolor or edgecolors matplotlib color spec or sequence of specs
facecolor or facecolors matplotlib color spec or sequence of specs
figure a matplotlib.figure.Figure instance
gid an id string
hatch [ ‘/’ | ‘\’ | ‘|’ | ‘-‘ | ‘+’ | ‘x’ | ‘o’ | ‘O’ | ‘.’ | ‘*’ ]
label string or anything printable with ‘%s’ conversion.
left_margin unknown
linestyle or dashes or linestyles [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
linewidth or linewidths or lw float or sequence of floats
margins unknown
norm unknown
offset_position unknown
offsets float or sequence of floats
path_effects unknown
picker [None|float|boolean|callable]
pickradius unknown
rasterized [True | False | None]
right_margin unknown
sketch_params unknown
snap unknown
top_margin unknown
transform Transform instance
url a url string
urls unknown
visible [True | False]
zorder any number

The return value is a PolyCollection instance; use get_array() on this PolyCollection to get the counts in each hexagon. If marginals is True, horizontal bar and vertical bar (both PolyCollections) will be attached to the return collection as attributes hbar and vbar.

Example:

(Source code, png, pdf)

../../_images/hexbin_demo.png

Notes

In addition to the above described arguments, this function can take a data keyword argument. If such a data argument is given, the following arguments are replaced by data[<arg>]:

  • All arguments with the following names: ‘x’, ‘y’.