svgdow-hour
Weekday × hour heatmap
When in the week does activity happen?
How to read it
- Read rows as weekdays and columns as hours.
- Find the darkest block: when activity peaks.
- Compare weekdays with the weekend rows.
- Callout 1: Darkest block: the busiest hours.
- Callout 2: Weekend rows: a different pattern?
- Callout 3: Hours run along the bottom, in UTC.
Watch out
- Hours are in UTC here. Check that the time zone matches how you read the pattern.
Defaults
- Cell value
- Row count
- What each cell's colour shows, such as a count, a sum or a mean.
- Also: Mean of value
Pinned in the code
- Weekday and hour are taken in UTC; rows are ISO weekdays (Monday first), columns hours 00–23.
- Counts map 0 → max on a sequential ramp; means map min → max of the cell means. Empty cells (no rows for a mean) are grey.
- Convert timestamps to local time before charting if local rhythms matter.
- All calendar arithmetic is in UTC; timestamps are epoch milliseconds.
# Weekday × hour heatmap — Codesplash Chart Lab (recipe "dow-hour")
# controls: agg=count
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("dow-hour-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["started_at"] = pd.to_datetime(df["started_at"])
_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")
AGG = "count" # "count" rows, or "mean" of the value
d = df.dropna(subset=["started_at"])
dow, hour = d["started_at"].dt.day_of_week, d["started_at"].dt.hour # UTC; Monday = 0
cells = d.groupby([dow, hour])["session_minutes"].mean() if AGG == "mean" else d.groupby([dow, hour]).size()
grid = cells.unstack().reindex(index=range(7), columns=range(24))
if AGG != "mean":
grid = grid.fillna(0) # empty cells count 0; empty means stay blank (grey)
cmap = LinearSegmentedColormap.from_list("seq", SEQ)
cmap.set_bad(GREY)
fig, ax = plt.subplots(figsize=(9, 3.2))
im = ax.imshow(grid.to_numpy(dtype=float), cmap=cmap, aspect="auto", vmin=0 if AGG != "mean" else None)
ax.set_yticks(range(7), ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"])
ax.set_xticks(range(24), [f"{h:02d}" for h in range(24)], fontsize=7)
ax.set_xlabel("hour (UTC)")
ax.grid(False)
fig.colorbar(im, ax=ax, label="mean " + "session_minutes" if AGG == "mean" else "rows")
plt.show()Needs pandas, numpy, matplotlib · reads dow-hour-demo.csv · app theme from the optional codesplash helper
# Weekday × hour heatmap — Codesplash Chart Lab (recipe "dow-hour")
# controls: agg=count
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
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 px.colors.qualitative.Plotly
df = pd.read_csv("dow-hour-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["started_at"] = pd.to_datetime(df["started_at"])
_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")
AGG = "count"
d = df.dropna(subset=["started_at"])
dow, hour = d["started_at"].dt.day_of_week, d["started_at"].dt.hour
cells = d.groupby([dow, hour])["session_minutes"].mean() if AGG == "mean" else d.groupby([dow, hour]).size()
grid = cells.unstack().reindex(index=range(7), columns=range(24))
if AGG != "mean":
grid = grid.fillna(0)
fig = go.Figure(go.Heatmap(z=grid.to_numpy(dtype=float), x=[f"{h:02d}" for h in range(24)], y=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], colorscale=[[0, SEQ[0]], [1, SEQ[-1]]], xgap=1, ygap=1))
fig.update_layout(yaxis_autorange="reversed", xaxis_title="hour (UTC)")
fig.show()Needs pandas, numpy, plotly · reads dow-hour-demo.csv · app theme from the optional codesplash helper
# Weekday × hour heatmap — Codesplash Chart Lab (recipe "dow-hour")
# controls: agg=count
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("dow-hour-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["started_at"]] <- as.POSIXct(df[["started_at"]], tz = "UTC")
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
agg <- "count" # "count" rows, or "mean" of the value
t <- as.POSIXlt(df[["started_at"]], tz = "UTC")
ok <- !is.na(t)
dow <- (t$wday[ok] + 6) %% 7; hour <- t$hour[ok] # UTC; Monday = 0
cells <- expand.grid(dow = 0:6, hour = 0:23)
key <- paste(dow, hour)
vals <- if (agg == "mean") tapply(df[["session_minutes"]][ok], key, mean, na.rm = TRUE) else tapply(key, key, length)
cells$value <- as.numeric(vals[paste(cells$dow, cells$hour)])
if (agg != "mean") cells$value[is.na(cells$value)] <- 0 # empty means stay blank (grey)
cells$dow <- factor(cells$dow, levels = 0:6, labels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
p <- ggplot(cells, aes(hour, dow, fill = value)) +
geom_tile(colour = "white", linewidth = 0.3) +
scale_fill_gradient(low = seq_ramp[1], high = seq_ramp[2], na.value = grey_col, name = if (agg == "mean") paste("mean", "session_minutes") else "rows") +
scale_y_discrete(limits = rev) + scale_x_continuous(breaks = seq(0, 23, 3)) +
labs(x = "hour (UTC)", y = NULL) +
theme_cs + theme(panel.grid = element_blank())
print(p)Needs ggplot2 · reads dow-hour-demo.csv · app theme from the optional codesplashr helper
-- Weekday × hour heatmap — Codesplash Chart Lab (recipe "dow-hour")
-- controls: agg=count
-- 7 × 24 grid by UTC weekday (Monday = 0) and hour, including empty cells
WITH r AS (
SELECT isodow("started_at") - 1 AS dow, hour("started_at") AS hour, "session_minutes" AS v
FROM read_csv('dow-hour-demo.csv') WHERE "started_at" IS NOT NULL
)
SELECT g.dow, h.hour, count(r.dow) AS n, avg(r.v) AS mean
FROM range(7) g(dow) CROSS JOIN range(24) h(hour)
LEFT JOIN r ON r.dow = g.dow AND r.hour = h.hour
GROUP BY ALL
ORDER BY ALL;Needs duckdb · reads dow-hour-demo.csv