Chart Gallery

/

Hexbin Plot in Python

Hexbin Plots in Python

A hexbin plot (also called a hexbin chart or hexbin graph) in python aggregates a dense point cloud into a hexagonal grid, coloring each cell by how many points fall inside it. With xy you build an interactive hexbin from millions of points without overplotting: pan, zoom, and hover work by default, and dense regions stay legible.

Jump to creating a hexbin, value channels, or the options table.

Create a Hexbin Plot

Pass paired x and y arrays to hexbin. It aggregates large point clouds into a grid of hexagonal cells, so a scatter that would smear into a solid blob becomes a readable density map:

Tune the Grid and Add a Colorbar

Pass a (nx, ny) tuple to gridsize to control cell resolution per axis, raise mincnt to clear out sparse background cells, swap the colormap, and add an xy.colorbar() so the count scale is readable:

Color Cells by a Mean Value

Instead of counting points, pass a C value array and a reduce_C_function such as np.mean — each cell is colored by the reduced value of the samples that land in it, turning scattered measurements into a surface:

Count Bins and Value Channels

By default each cell is colored by its point count. Raise gridsize for finer cells and use mincnt to hide sparse cells below a threshold. To color by a quantity instead of a raw count, pass a C value array and set reduce_C_function (for example np.mean) to reduce the values that fall in each cell into a single color.

Hexbin Options

OptionPurpose
gridsizeNumber of hexagons across; higher means finer cells.
binsColor-scale binning, e.g. "log" for skewed counts.
mincntMinimum points a cell needs to be drawn.
COptional value array reduced per cell instead of counting points.
reduce_C_functionReducer applied to C in each cell, e.g. np.mean.
colormapNamed color scale mapping cell values to color.
opacityCell opacity from 0 to 1.

Add to show the value-to-color scale. Pass column names with data= instead of arrays when your data is a table.

  • Heatmaps — color a regular grid of precomputed values.
  • Contour plots — draw iso-value lines over a density field.
  • Scatter charts — plot individual points when the cloud is small enough to read.

API Reference

xy.hexbin_chart

A hexbin chart composing `hexbin` marks.

Props

PropTypeDescription
*childrenComponent

Marks, axes, annotations, and chart chrome.

titleOptional[str]

Title shown above the plot.

widthint | str

Chart width in pixels or a CSS size such as ``"100%"``.

heightint | str

Chart height in pixels or a CSS size such as ``"100%"``.

paddingUnion[float, Sequence[float], None]

Plot margins, as one value or a sequence of side values. Use zero for an edge-to-edge sparkline.

dataTableLike

Chart-level data used by marks that omit their own ``data``.

class_nameOptional[str]

CSS class applied to the chart container.

class_namesOptional[dict[str, str]]

CSS classes keyed by stable chart DOM slot.

styleOptional[dict[str, StyleValue]]

Inline style overrides for the chart container.

stylesOptional[dict[str, dict[str, StyleValue]]]

Inline style mappings keyed by stable chart DOM slot.

on_hoverOptional[Callable[[dict], None]]

Callback receiving hover event payloads.

on_clickOptional[Callable[[dict], None]]

Callback receiving picked-mark click payloads.

on_brushOptional[Callable[[dict], None]]

Callback receiving brush event payloads.

on_selectOptional[Callable[[Selection], None]]

Callback receiving data-space selections.

on_view_changeOptional[Callable[[dict], None]]

Callback receiving viewport change payloads.

hoverOptional[bool]

Whether pointer movement emits hover events.

clickOptional[bool]

Whether picked marks emit click events.

selectOptional[bool]

Whether shift-drag box selection is enabled.

brushOptional[bool]

Whether brush selection is enabled.

crosshairOptional[bool]

Whether plot-aligned hover guides are shown.

navigationOptional[bool]

Whether browser pan and zoom navigation is enabled.

panOptional[bool]

Whether plain-drag panning is enabled.

pan_axesOptional[tuple[str, ...]]

Declared axis IDs translated by pan gestures.

zoomOptional[bool]

Whether viewport zoom is enabled.

default_drag_actionOptional[DefaultDragAction]

Initial action performed by a plain plot drag.

zoom_axesOptional[tuple[str, ...]]

Declared axis IDs changed by zoom gestures and controls.

zoom_limitsOptional[ZoomLimits]

Minimum and maximum magnification globally or by axis.

wheel_zoomOptional[bool]

Whether wheel and trackpad zoom is available.

box_zoomOptional[bool]

Whether box zoom is available as a drag action.

zoom_buttonsOptional[bool]

Whether modebar Zoom In/Out commands are available.

double_click_resetOptional[bool]

Whether double-click restores ``reset_axes``.

reset_axesOptional[tuple[str, ...]]

Declared axis IDs restored by reset.

link_groupOptional[str]

Identifier used to synchronize charts in the browser.

link_axesOptional[tuple[str, ...]]

Axes synchronized within the link group.

FAQ

How do I make a hexbin plot in Python?

Pass paired x and y arrays to xy.hexbin(x, y) inside xy.hexbin_chart(...) and render it. Cells are colored by point count automatically.

When should I use a hexbin instead of a scatter?

Use a hexbin when a scatter overplots — tens of thousands of points or more that smear into a solid mass. Hexbin aggregates them into a density grid you can read.

How do I color a hexbin by a value instead of a count?

Pass a C value array and set reduce_C_function, such as np.mean, to reduce the values in each cell into a single color.

How do I hide sparse hexagons?

Set mincnt to the minimum number of points a cell must contain before it is drawn, which clears out near-empty background cells.

Built with Reflex