SPB Git
1.8 KB · 34 lines python
1#!/usr/bin/env python32"""Ch. 30, figure « tornade » : variation de la valeur DCF du cas 3 (DONNÉES SYNTHÉTIQUES) pour un choc bas/haut de chaque3intrant, les autres étant fixés à leur valeur de base. Sortie : ch30-fig-tornade.pdf."""4from __future__ import annotations5import sys, pathlib6sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))7import numpy as np, matplotlib.pyplot as plt8from matplotlib.ticker import FuncFormatter9from traite.style import setup, palette, savefig, LARGEUR, fmt_milliers10from cases import ch30_dcf as c1112def main():13    setup()14    r = c.base(); t = c.tornade(r); V0 = r["dcf"]["valeur"]15    t = t.iloc[::-1].reset_index(drop=True)  # plus grande amplitude en haut16    fig, ax = plt.subplots(figsize=(LARGEUR, 3.6))17    y = np.arange(len(t))18    ax.barh(y, t["delta_bas"] / 1000, color=palette.cat[0], label="Borne basse de l'intrant")19    ax.barh(y, t["delta_haut"] / 1000, color=palette.cat[1], label="Borne haute de l'intrant")20    ax.axvline(0, color=palette.ink, lw=0.9)21    ax.set_yticks(y); ax.set_yticklabels(t["intrant"], fontsize=7.8)22    for yi, (db, dh) in enumerate(zip(t["delta_bas"], t["delta_haut"])):23        for dv in (db, dh):24            ax.text(dv / 1000 + (6 if dv >= 0 else -6), yi, f"{dv/1000:+,.0f}".replace(",", " "), va="center", ha="left" if dv >= 0 else "right", fontsize=7, color=palette.ink2)25    ax.set_xlabel(f"Variation de la valeur par rapport à la base ({fmt_milliers(V0)} \\$), en milliers de \\$")26    lim = max(abs(t["delta_bas"].min()), abs(t["delta_haut"].max())) / 1000 * 1.2527    ax.set_xlim(-lim, lim)28    ax.xaxis.set_major_formatter(FuncFormatter(lambda v, q: f"{v:+,.0f}".replace(",", " ")))29    ax.legend(loc="lower right")30    savefig(fig, "ch30/ch30-fig-tornade.pdf")3132if __name__ == "__main__":33    main()34
Files · 8cb0de6f5c8171b14f35a0fe50fcc68c0097664d
Menu

Theme