Skip to content

Instantly share code, notes, and snippets.

@cavedave
Created November 25, 2026 20:06
Show Gist options
  • Select an option

  • Save cavedave/e37d3e1c028900bacb6f469cecbae5ce to your computer and use it in GitHub Desktop.

Select an option

Save cavedave/e37d3e1c028900bacb6f469cecbae5ce to your computer and use it in GitHub Desktop.
"""Daily global temperature anomaly (ERA5, Copernicus Climate Pulse) - linear bar chart.
Anomaly is relative to the 1991-2020 daily climatology, as published (no offset).
Output: anomaly_linear.png
"""
from pathlib import Path
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.colors import LinearSegmentedColormap, TwoSlopeNorm
HERE = Path(__file__).parent
df = pd.read_csv(HERE / "data/era5_daily_series_2t_global.csv", comment="#", parse_dates=["date"])
anom = df["ano_91-20"].to_numpy()
# RdBu_r with its white middle cut out, so even near-average days are clearly blue or red
_base = plt.get_cmap("RdBu_r")
cmap = LinearSegmentedColormap.from_list(
"RdBu_no_white", np.vstack([_base(np.linspace(0, 0.33, 128)), _base(np.linspace(0.67, 1, 128))]))
norm = TwoSlopeNorm(vmin=-1.0, vcenter=0.0, vmax=1.35) # 0 = 1991-2020 average
rec = df.loc[df["ano_91-20"].idxmax()]
CREDIT = "Data: ERA5 via Copernicus Climate Pulse (C3S/ECMWF). 0 = average of the years 1991–2020."
fig, ax = plt.subplots(figsize=(14, 5))
# 31,000+ days don't fit in ~2,000 pixels, so per-day bars alias into white gaps.
# Instead: fill the area between 0 and the daily values, and paint it with a
# vertical colour gradient (colour = how far above/below average).
x = mdates.date2num(df["date"])
area = ax.fill_between(x, 0, anom, color="none", linewidth=0)
ylo, yhi = anom.min() - 0.05, anom.max() + 0.05
grad = np.linspace(ylo, yhi, 512).reshape(-1, 1)
img = ax.imshow(grad, cmap=cmap, norm=norm, origin="lower", aspect="auto",
extent=(x[0], x[-1], ylo, yhi), interpolation="bilinear")
img.set_clip_path(area.get_paths()[0], transform=ax.transData)
ax.set_ylim(ylo - 0.1, yhi + 0.1)
ax.xaxis_date()
pad = (x[-1] - x[0]) * 0.01
ax.set_xlim(x[0] - pad, x[-1] + pad)
ax.axhline(0, color="0.3", linewidth=0.8)
ax.text(df["date"].iloc[0], 0.03, "1991–2020 average", fontsize=9, color="0.25")
ax.annotate(f"Record: {rec['date']:%d %b %Y}\n+{rec['ano_91-20']:.2f} °C",
xy=(rec["date"], rec["ano_91-20"]), xytext=(-150, -5), textcoords="offset points",
arrowprops=dict(arrowstyle="->", color="0.2"), fontsize=10)
ax.set_ylabel("°C above / below 1991–2020 average")
ax.set_title("Daily global temperature compared with the 1991–2020 average, 1940–2026")
for s in ("top", "right"):
ax.spines[s].set_visible(False)
fig.text(0.01, 0.01, CREDIT, fontsize=8, color="0.4")
fig.tight_layout(rect=(0, 0.03, 1, 1))
fig.savefig(HERE / "anomaly_linear.png", dpi=150)
print("saved")
@cavedave

Copy link
Copy Markdown
Author
anomaly_linear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment