Skip to content
svgcalendar-heatmap

Calendar heatmap

Which days are busy or quiet across a whole year — weekly rhythms, holidays, gaps?

Demo data · seed 20261101 · 1,096 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read a row as a weekday and a column as a week.
  2. Look for columns or rows that stand out: holidays and weekly rhythms.
  3. Blank cells mean no data, which is different from zero.
  1. Callout 1: Rows are weekdays.
  2. Callout 2: A block of darker days.
  3. Callout 3: Colour scale: the daily value.

Watch out

  • An extreme day can wash out the rest of a linear colour scale.

Defaults

Daily value
Sum
How rows combine into one number per day.
Also: Mean · Row count
Colour scale
Linear (0 → max)
Linear shows the dense cells; log or square root reveals the sparse ones too.
Also: Quantile (rank)
Years shown (latest)
3
How many of the latest years to show.
Range 1 to 5, step 1

Pinned in the code

  • Rows are aggregated per UTC calendar day (sum, mean or count).
  • Columns are weeks counted from the Monday on or before 1 January; rows are ISO weekdays (Monday first).
  • Linear colour maps 0 → the maximum day; quantile colour maps each day's rank among days with data (robust to outliers).
  • Days inside the data range without rows are drawn as empty cells; days outside it are omitted.
  • All calendar arithmetic is in UTC; timestamps are epoch milliseconds.
# Calendar heatmap — Codesplash Chart Lab (recipe "calendar-heatmap")
# controls: agg=sum, scale=linear, years=3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("calendar-heatmap-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["date"] = pd.to_datetime(df["date"])

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
def daily_values(d, time, value, agg):
    """Daily sum / mean / row count per UTC calendar day."""
    day = d[time].dt.floor("D")
    if value is None or agg == "count":
        return d.groupby(day).size().astype(float)
    return d.groupby(day)[value].agg(agg)

def calendar_grid(daily, year):
    """7 × 54 grid: columns are weeks from the Monday on or before 1 January, rows ISO weekdays."""
    jan1 = pd.Timestamp(year=year, month=1, day=1)
    origin = jan1 - pd.Timedelta(days=jan1.day_of_week)
    grid = np.full((7, 54), np.nan)
    inside = np.zeros((7, 54), bool)
    for day in pd.date_range(max(daily.index.min(), jan1), min(daily.index.max(), pd.Timestamp(year=year, month=12, day=31)), freq="D"):
        w, dow = (day - origin).days // 7, day.day_of_week
        inside[dow, w] = True  # days inside the data range without rows stay empty
        if day in daily.index:
            grid[dow, w] = daily[day]
    return grid, inside

AGG, SCALE, YEARS = "sum", "linear", 3
d = df.dropna(subset=["date", "commits"])
daily = daily_values(d, "date", "commits", AGG)
if SCALE == "quantile":  # each day's rank among days with data (robust to outliers)
    daily = daily.rank(pct=True)
years = sorted(daily.index.year.unique())[-YEARS:]
cmap = LinearSegmentedColormap.from_list("seq", SEQ)
vmax = 1 if SCALE == "quantile" else daily.max()  # linear: 0 → the maximum day
fig, axes = plt.subplots(len(years), 1, figsize=(11, 1.7 * len(years)), squeeze=False)
for ax, year in zip(axes[:, 0], years):
    grid, inside = calendar_grid(daily, year)
    ax.imshow(np.where(inside, 0.0, np.nan), cmap=LinearSegmentedColormap.from_list("e", [GREY, GREY]), aspect="equal", alpha=0.35)
    im = ax.imshow(grid, cmap=cmap, vmin=0, vmax=vmax, aspect="equal")
    ax.set_yticks(range(7), ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], fontsize=7)
    jan1 = pd.Timestamp(year=year, month=1, day=1)
    origin = jan1 - pd.Timedelta(days=jan1.day_of_week)
    starts = [pd.Timestamp(year=year, month=m, day=1) for m in range(1, 13)]
    ax.set_xticks([(s - origin).days // 7 for s in starts], [s.strftime("%b") for s in starts], fontsize=7)
    ax.set_title(str(year), fontsize=9, loc="left")
    ax.grid(False)
fig.colorbar(im, ax=axes[:, 0].tolist(), shrink=0.6, label=AGG + " of " + "commits" if AGG != "count" else "rows")
plt.show()

Needs pandas, numpy, matplotlib · reads calendar-heatmap-demo.csv · app theme from the optional codesplash helper