|
| 1 | +# Temperature anomaly |
| 2 | + |
| 3 | +<video controls="true" allowfullscreen="true"> |
| 4 | +<source src="https://github.com/ahuang11/streamjoy/assets/15331990/069b1826-de92-4643-8be5-6d5a5301d11e" type="video/mp4"> |
| 5 | +</video> |
| 6 | + |
| 7 | +Shows the global temperature anomaly from 1995 to 2024 using the HadCRUT5 dataset. The video pauses at notable dates. |
| 8 | + |
| 9 | +Highlights: |
| 10 | + |
| 11 | +- Uses `wrap_matplotlib` to automatically handle saving and closing the figure. |
| 12 | +- Uses a custom `renderer` function to create each frame of the animation. |
| 13 | +- Uses `Paused` to pause the animation at notable dates. |
| 14 | + |
| 15 | +```python |
| 16 | +import pandas as pd |
| 17 | +import matplotlib.pyplot as plt |
| 18 | +from streamjoy import stream, wrap_matplotlib, Paused |
| 19 | + |
| 20 | +URL = "https://climexp.knmi.nl/data/ihadcrut5_global.dat" |
| 21 | +NOTABLE_DATES = { |
| 22 | + "1997-12": "Kyoto Protocol adopted", |
| 23 | + "2005-01": "Exceeded 380 ppm", |
| 24 | + "2010-01": "Exceeded 390 ppm", |
| 25 | + "2013-05": "Exceeded 400 ppm", |
| 26 | + "2015-12": "Paris Agreement signed", |
| 27 | + "2016-01": "CO2 permanently over 400 ppm", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +@wrap_matplotlib() |
| 32 | +def renderer(df): |
| 33 | + plt.style.use("dark_background") # Setting the style for dark mode |
| 34 | + |
| 35 | + fig, ax = plt.subplots() |
| 36 | + fig.patch.set_facecolor("#1b1e23") |
| 37 | + ax.set_facecolor("#1b1e23") |
| 38 | + ax.set_frame_on(False) |
| 39 | + ax.axis("off") |
| 40 | + |
| 41 | + # Set title |
| 42 | + year = df["year"].iloc[-1] |
| 43 | + ax.set_title( |
| 44 | + f"Global Temperature Anomaly {year} [HadCRUT5]", |
| 45 | + fontsize=15, |
| 46 | + loc="left", |
| 47 | + fontname="Courier New", |
| 48 | + color="lightgrey", |
| 49 | + ) |
| 50 | + |
| 51 | + # draw line |
| 52 | + df.groupby("year")["anom"].plot( |
| 53 | + y="anom", color="lightgrey", legend=False, ax=ax, lw=0.5 |
| 54 | + ) |
| 55 | + |
| 56 | + # add source text at bottom right |
| 57 | + ax.text( |
| 58 | + 0.01, |
| 59 | + 0.05, |
| 60 | + f"Source: {URL}", |
| 61 | + va="bottom", |
| 62 | + ha="left", |
| 63 | + transform=ax.transAxes, |
| 64 | + fontsize=8, |
| 65 | + color="lightgrey", |
| 66 | + fontname="Courier New", |
| 67 | + ) |
| 68 | + |
| 69 | + # draw end point |
| 70 | + jday = df.index.values[-1] |
| 71 | + anom = df["anom"].values[-1] |
| 72 | + ax.scatter(jday, anom, color="red", zorder=999) |
| 73 | + anom_label = f"+{anom:.1f} K" if anom > 0 else f"{anom:.1f} K" |
| 74 | + ax.annotate( |
| 75 | + anom_label, |
| 76 | + (jday, anom), |
| 77 | + textcoords="offset points", |
| 78 | + xytext=(-10, 5), |
| 79 | + fontsize=12, |
| 80 | + ha="right", |
| 81 | + va="bottom", |
| 82 | + color="lightgrey", |
| 83 | + ) |
| 84 | + |
| 85 | + # draw yearly labels |
| 86 | + for year, df_year in df.reset_index().groupby("year").last().iloc[-5:].iterrows(): |
| 87 | + if df_year["month"] != 12: |
| 88 | + continue |
| 89 | + ax.annotate( |
| 90 | + year, |
| 91 | + (df_year["jday"], df_year["anom"]), |
| 92 | + fontsize=12, |
| 93 | + ha="left", |
| 94 | + va="center", |
| 95 | + color="lightgrey", |
| 96 | + fontname="Courier New", |
| 97 | + ) |
| 98 | + |
| 99 | + plt.subplots_adjust(bottom=0, top=0.9, left=0.05) |
| 100 | + |
| 101 | + month = df["date"].iloc[-1].strftime("%b") |
| 102 | + ax.annotate( |
| 103 | + month, |
| 104 | + (jday, anom), |
| 105 | + textcoords="offset points", |
| 106 | + xytext=(-10, 3), |
| 107 | + fontsize=12, |
| 108 | + ha="right", |
| 109 | + va="top", |
| 110 | + color="lightgrey", |
| 111 | + fontname="Courier New", |
| 112 | + ) |
| 113 | + date_string = df["date"].iloc[-1].strftime("%Y-%m") |
| 114 | + if date_string in NOTABLE_DATES: |
| 115 | + ax.annotate( |
| 116 | + f"{NOTABLE_DATES[date_string]}", |
| 117 | + xy=(0, 1), |
| 118 | + xycoords="axes fraction", |
| 119 | + xytext=(0, -5), |
| 120 | + textcoords="offset points", |
| 121 | + fontsize=12, |
| 122 | + ha="left", |
| 123 | + va="top", |
| 124 | + color="lightgrey", |
| 125 | + fontname="Courier New", |
| 126 | + ) |
| 127 | + return Paused(fig, 3) |
| 128 | + return fig |
| 129 | + |
| 130 | + |
| 131 | +df = ( |
| 132 | + pd.read_csv( |
| 133 | + URL, |
| 134 | + comment="#", |
| 135 | + header=None, |
| 136 | + sep="\s+", |
| 137 | + na_values=[-999.9], |
| 138 | + ) |
| 139 | + .rename(columns={0: "year"}) |
| 140 | + .melt(id_vars="year", var_name="month", value_name="anom") |
| 141 | +) |
| 142 | +df.index = pd.to_datetime( |
| 143 | + df["year"].astype(str) + df["month"].astype(str), format="%Y%m" |
| 144 | +) |
| 145 | +df = df.sort_index()["1995":"2024"] |
| 146 | +df["jday"] = df.index.dayofyear |
| 147 | +df = df.rename_axis("date").reset_index().set_index("jday") |
| 148 | +df_list = [df[:i] for i in range(1, len(df) + 1)] |
| 149 | + |
| 150 | +stream(df_list, renderer=renderer, threads_per_worker=1).write( |
| 151 | + "temperature_anomaly.mp4" |
| 152 | +) |
| 153 | +``` |
0 commit comments