# 1) find every record‐breaking year ≥ 1980 post80 = ts.where(ts.index >= 1980) current_max = -np.inf record_years = [] record_values = [] for yr, val in post80.items(): if np.isnan(val): continue if val >= current_max: current_max = val record_years.append(yr) record_values.append(val) # 2) build ALL steps all_steps = [(s, e, y) for s, e, y in zip(record_years, record_years[1:], record_values)] # 3) filter to only multi‑year “flat” periods (>=3 yr) steps = [(s, e, y) for (s, e, y) in all_steps if (e - s) >= 3] # 4) pre‑compute limits & frame list xmin, xmax = ts.index.min(), ts.index.max() ymin, ymax = ts.min() - 0.1, ts.max() + 0.1 frame_files = []