import datashader as ds import datashader.transfer_functions as tf★Two imports do almost everything:dsfor aggregation,tffor turning aggregates into images.# 3 stages: aggregate (Canvas) -> transform -> shade (tf) agg = cvs.points(df, "x", "y") img = tf.shade(agg)★The whole model in two lines. Aggregation produces an xarray grid; shading colors it. Each pixel is an honest aggregate — no overplotting, no sampling.pip install datashader # brings numba, xarray, daskAggregation is JIT-compiled (Numba), so it's fast on tens of millions of rows on a laptop.
cvs = ds.Canvas(plot_width=800, plot_height=600, x_range=(0, 10), y_range=(0, 10))★The grid = plot_width × plot_height pixels. Setx_range/y_rangeto fix the extent (and avoid a scan to infer it — matters for Dask).agg = cvs.points(df, "lon", "lat", agg=ds.count())★Scatter/points — each row drops into one cell. Theagg=reduction (card 3) decides what each cell holds.cvs.line(df, "t", "value") # timeseries / trajectories cvs.raster(xarr) · cvs.polygons(...) · cvs.area(...) · cvs.quadmesh(...)Other glyphs:line(connected series),raster/quadmesh(regridding),polygons,trimesh(unstructured meshes).
ds.count() # rows per cell (default) ds.mean("speed") · ds.sum("amt") · ds.max("z")★Pick the statistic each pixel aggregates. Alsomin,std,var,first,last,any.ds.by("category", ds.count()) # per-category stacked grid★Categorical aggregation — separate count per category, later colored by acolor_key(card 5). The column must becategorydtype. (Older API:ds.count_cat.)ds.summary(n=ds.count(), avg=ds.mean("v"))Compute several reductions in one pass — returns an xarray Dataset with each as a variable.