Note
Go to the end to download the full example code.
Shifted Distribution Plot¶
Compare how a variable’s distribution has shifted between a reference and a comparison period. Zone boundaries are derived automatically from the reference period’s mean and standard deviation (Hansen et al. methodology: +-1sigma and +-3sigma).
Best for: Detecting long-term shifts in climate variables, comparing two decades.
import diive as dv
Load example data¶
df = dv.load_exampledata_parquet()
series = df['Tair_f'].copy()
series.name = 'Air Temperature (deg C)'
print(f"Loaded {len(series)} records from {series.index[0]} to {series.index[-1]}")
print(f"Mean: {series.mean():.2f} deg C, Std: {series.std():.2f} deg C")
> 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.050
seconds).
Loaded 175296 records from 2013-01-01 00:15:00 to 2022-12-31 23:45:00
Mean: 4.71 deg C, Std: 7.55 deg C
Create shifted distribution plot¶
Reference period: 2013-2016 (first 4 years) Comparison period: 2019-2022 (last 4 years)
The gray hatched curve shows the reference distribution. The colored filled curve shows the comparison distribution, with zones colored from blue (cold) to red (hot).
sdp = dv.plotting.ShiftedDistributionPlot(
series=series,
ref_period=('2013', '2016'),
comp_period=('2019', '2022'),
)
print(f"\nBreakpoints (from reference period):")
labels = ['Extremely cold / Cold', 'Cold / Normal', 'Normal / Hot', 'Hot / Extremely hot']
for label, bp in zip(labels, sdp.breakpoints):
print(f" {label}: {bp:.2f} deg C")
# The plot's chrome (title, x-label, fonts, colours, grid) now lives in a shared
# FormatStyle. The colored patch legend and axis-visibility toggles stay direct
# arguments because they are specific to this plot, not part of the shared style.
style = dv.plotting.FormatStyle(
title='Air Temperature Distribution Shift',
xlabel='Air Temperature (deg C)',
)
sdp.plot(
ax=None,
format_style=style,
ref_label='Reference 2013-2016',
comp_label='Comparison 2019-2022',
show_legend=True,
show_title=True,
show_xaxis=True, # set False to hide bottom spine, ticks, and tick labels
show_yaxis=False, # set False to hide left spine, ticks, and tick labels
figsize=(16, 7),
)

Breakpoints (from reference period):
Extremely cold / Cold: -17.37 deg C
Cold / Normal: -2.77 deg C
Normal / Hot: 11.82 deg C
Hot / Extremely hot: 26.41 deg C
Total running time of the script: (0 minutes 3.170 seconds)