Note
Go to the end to download the full example code.
Anomaly Visualization¶
Long-term anomalies per year compared to a reference period. Shows deviations from climatological mean as bar plots (red=above reference, blue=below). Reference period mean ± standard deviation provided for context.
Best for: Detecting long-term trends, identifying extreme years, climate analysis
import diive as dv
Load example data¶
df = dv.load_exampledata_parquet()
print(f"Loaded {len(df)} records from {df.index[0].date()} to {df.index[-1].date()}")
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.049
seconds).
Loaded 175296 records from 2013-01-01 to 2022-12-31
Calculate annual air temperature¶
Aggregate daily data to annual means for anomaly calculation. Anomalies show deviations from a reference period baseline.
series = df['Tair_f'].copy()
series = series.resample('YE').mean()
series.index = series.index.year
series_label = "Air temperature"
Temperature anomalies with reference period¶
Visualize yearly anomalies relative to a reference period (2015-2017). Red bars indicate years warmer than the reference mean, blue bars indicate cooler years. Reference statistics and last 10 years summary displayed in the plot.
anomaly_plot = dv.plotting.LongtermAnomaliesYear(
series=series, # Annual mean temperature per year
series_label=series_label, # Variable name for plot title
series_units='(°C)', # Units appended to y-axis label
reference_start_year=2015, # First year of reference period
reference_end_year=2017 # Last year of reference period
)
# Chrome (here just the title) goes into a shared FormatStyle; data-specific
# settings like the series units stay on the plot class constructor above.
style = dv.plotting.FormatStyle(
title='Annual Air Temperature Anomalies (2013-2022)' # Custom title
)
anomaly_plot.plot(
ax=None, # Create new figure
format_style=style # Reusable chrome (title, fonts, colours, ...)
)
print("\nPlotted air temperature anomalies")
print(f"Reference period: 2015-2017 ({2017 - 2015 + 1} years)")

Plotted air temperature anomalies
Reference period: 2015-2017 (3 years)
Statistics¶
print(f"\nTemperature statistics:")
print(f" Annual mean: {series.mean():.2f}°C")
print(f" Annual std dev: {series.std():.2f}°C")
print(f" Warmest year: {series.idxmax()} ({series.max():.2f}°C)")
print(f" Coolest year: {series.idxmin()} ({series.min():.2f}°C)")
print(f" Range: {series.max() - series.min():.2f}°C")
Temperature statistics:
Annual mean: 4.71°C
Annual std dev: 0.60°C
Warmest year: 2022 (5.61°C)
Coolest year: 2013 (3.74°C)
Range: 1.87°C
Total running time of the script: (0 minutes 0.257 seconds)