Minimal call
import random
rng = random.Random(13)
xs = [rng.gauss(0, 1) for _ in range(3000)]
df = {"x": xs, "y": [x + rng.gauss(0, 1) for x in xs]}
c = pt.chart(df, aes(x="x", y="y"), xlabel="x", ylabel="y")
c.add_hexbin(gridsize=20)
Worked example
import random
import plotlet as pt
from plotlet import aes
rng = random.Random(13)
xs = [rng.gauss(0, 1) + rng.gauss(0, 0.4) for _ in range(3000)]
df = {"x": xs, "y": [x + rng.gauss(0, 1) for x in xs]}
hexes = pt.chart(df, aes(x="x", y="y"),
title="3000 points, binned", xlabel="x", ylabel="y",
data_width=300, data_height=260)
hexes.add_hexbin(gridsize=22)
c = hexes | pt.legend(hexes)
Docstring
Hexagonal density binning for crowded scatter; cells colored by point count.
For dense 2-D scatter, hex binning beats overplotting. Each hexagonal cell
counts the points that fall in it; cells are colored by count via a sequential
colormap. Used wherever a c.add_scatter() would devolve into a black blob.
Hexagons are sized and binned in pixel space so they always appear as regular
hexagons that tile without gaps regardless of the canvas aspect ratio.
API: c.add_hexbin(aes(x="col", y="col"))
Styling kwargs:
gridsize=30 number of hex columns across the canvas width
cmap='viridis' colormap name for coloring cells by count
vmin=0 colormap domain lower bound
vmax=None colormap domain upper bound (defaults to max count)