You've already forked etude_lego_jurassic_world
Ajouter la heatmap log des catégories et le collage des pièces imprimées
This commit is contained in:
@@ -101,6 +101,41 @@ def plot_part_categories_heatmap(categories_by_year_path: Path, destination_path
|
||||
plt.close(fig)
|
||||
|
||||
|
||||
def plot_part_categories_heatmap_log(categories_by_year_path: Path, destination_path: Path) -> None:
|
||||
"""Heatmap des quantités (log1p) par catégorie et par année, en excluant les catégories vides."""
|
||||
rows = load_rows(categories_by_year_path)
|
||||
years = extract_years(rows)
|
||||
totals: Dict[str, int] = {}
|
||||
quantity_lookup = {(row["year"], row["category_id"]): int(row["quantity_non_spare"]) for row in rows}
|
||||
for row in rows:
|
||||
totals[row["category_id"]] = totals.get(row["category_id"], 0) + int(row["quantity_non_spare"])
|
||||
categories = sorted([cat_id for cat_id, total in totals.items() if total > 0], key=lambda cat_id: -totals[cat_id])
|
||||
if not categories:
|
||||
return
|
||||
matrix = np.zeros((len(categories), len(years)))
|
||||
for i, cat_id in enumerate(categories):
|
||||
for j, year in enumerate(years):
|
||||
matrix[i, j] = np.log1p(quantity_lookup.get((year, cat_id), 0))
|
||||
fig, ax = plt.subplots(figsize=(12, 10))
|
||||
cmap = plt.get_cmap("magma")
|
||||
im = ax.imshow(matrix, aspect="auto", cmap=cmap, norm=Normalize(vmin=0, vmax=matrix.max() if matrix.max() > 0 else 1))
|
||||
ax.set_xticks(np.arange(len(years)))
|
||||
ax.set_xticklabels(years, rotation=45, ha="right")
|
||||
labels = {row["category_id"]: row["category_name"] for row in rows}
|
||||
ax.set_yticks(np.arange(len(categories)))
|
||||
ax.set_yticklabels([labels[cat_id] for cat_id in categories])
|
||||
ax.set_xlabel("Année")
|
||||
ax.set_ylabel("Catégorie de pièce")
|
||||
ax.set_title("Intensité des catégories de pièces par année (log des quantités)")
|
||||
cbar = fig.colorbar(ScalarMappable(norm=im.norm, cmap=cmap), ax=ax, fraction=0.025, pad=0.015)
|
||||
cbar.ax.set_ylabel("log1p(quantité)", rotation=90)
|
||||
|
||||
ensure_parent_dir(destination_path)
|
||||
fig.tight_layout()
|
||||
fig.savefig(destination_path, dpi=170)
|
||||
plt.close(fig)
|
||||
|
||||
|
||||
def plot_structural_share_timeline(categories_by_year_path: Path, destination_path: Path) -> None:
|
||||
"""Trace l'évolution de la part des catégories structurelles."""
|
||||
rows = load_rows(categories_by_year_path)
|
||||
|
||||
Reference in New Issue
Block a user