#!/usr/bin/env python3 """Figure ch21 — IPCB Montréal (Statistique Canada, tableau 18-10-0289-01, base 2023 = 100), données RÉELLES lues dans data/raw. Séries résidentielles depuis le début de la série (T1 2017) jusqu'au T3 2026 ; non résidentiel en comparaison. Sortie : assets/figures/ch21/ch21-fig-ipcb-montreal.pdf""" import sys, pathlib ROOT = pathlib.Path(__file__).resolve().parents[3] sys.path.insert(0, str(ROOT / "analysis/python")); sys.path.insert(0, str(ROOT / "analysis/python/cases")) import matplotlib.pyplot as plt, matplotlib.dates as mdates from traite.style import setup, palette, savefig, LARGEUR import ch21_cout_neuf as cas ETIQ = {"Bâtiments résidentiels [621]": "Bâtiments résidentiels", "Maison individuelle [621112]": "Maison individuelle", "Immeuble à hauteur restreinte (moins de cinq étages) [62111]": "Immeuble < 5 étages", "Tour d'habitations (cinq étages ou plus) [621111]": "Tour d'habitation (≥ 5 étages)", "Bâtiments non résidentiels [622]": "Non résidentiel"} def main(): setup() df = cas.ipcb_montreal(); df = df[df["date"] >= "2017-01-01"] fig, ax = plt.subplots(figsize=(LARGEUR, 3.5)) for i, (t, lab) in enumerate(ETIQ.items()): s = df[df["type"] == t] lw = 2.2 if t.startswith("Maison") else 1.4 ls = "--" if t.startswith("Bâtiments non") else "-" ax.plot(s["date"], s["indice"], color=palette.cat[i], lw=lw, ls=ls, label=lab) ax.axhline(100, color=palette.ink2, lw=0.6, ls=":"); ax.text(df["date"].min(), 101, "2023 = 100", fontsize=7.5, color=palette.ink2) ax.set_ylabel("Indice (2023 = 100)"); ax.set_xlim(df["date"].min(), df["date"].max() + __import__("pandas").Timedelta(days=120)) ax.legend(loc="upper left", bbox_to_anchor=(0.0, 0.98), fontsize=7.5) mi = df[df["type"] == "Maison individuelle [621112]"] ax.annotate(f"T3 2026 : {mi['indice'].iloc[-1]:.1f}", xy=(mi["date"].iloc[-1], mi["indice"].iloc[-1]), xytext=(-70, 14), textcoords="offset points", fontsize=7.5, arrowprops=dict(arrowstyle="-", color=palette.ink2, lw=0.6), color=palette.ink) ax.xaxis.set_major_locator(mdates.YearLocator()); ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y")) ax.set_ylim(60, 125) out = savefig(fig, "ch21/ch21-fig-ipcb-montreal.pdf"); print("OK", out) if __name__ == "__main__": main()