You've already forked etude_lego_jurassic_world
Ajoute le genre des personnages et colore les graphiques
This commit is contained in:
@@ -4,11 +4,25 @@ from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.patches import Patch
|
||||
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
from lib.rebrickable.stats import read_rows
|
||||
|
||||
|
||||
GENDER_COLORS = {
|
||||
"male": "#4c72b0",
|
||||
"female": "#c44e52",
|
||||
"unknown": "#7f7f7f",
|
||||
}
|
||||
GENDER_LABELS = {
|
||||
"male": "Homme",
|
||||
"female": "Femme",
|
||||
"unknown": "Inconnu",
|
||||
"": "Inconnu",
|
||||
}
|
||||
|
||||
|
||||
def load_counts(path: Path) -> List[dict]:
|
||||
"""Charge le CSV des comptes par personnage."""
|
||||
return read_rows(path)
|
||||
@@ -24,11 +38,13 @@ def plot_minifigs_per_character(counts_path: Path, destination_path: Path) -> No
|
||||
rows = load_counts(counts_path)
|
||||
characters = [row["known_character"] for row in rows]
|
||||
counts = [int(row["minifig_count"]) for row in rows]
|
||||
genders = [row.get("gender", "") for row in rows]
|
||||
colors = [GENDER_COLORS.get(gender.strip().lower(), GENDER_COLORS["unknown"]) for gender in genders]
|
||||
positions = list(range(len(rows)))
|
||||
height = max(6, len(rows) * 0.22)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, height))
|
||||
bars = ax.barh(positions, counts, color="#1f77b4", edgecolor="#0d0d0d", linewidth=0.6)
|
||||
bars = ax.barh(positions, counts, color=colors, edgecolor="#0d0d0d", linewidth=0.6)
|
||||
ax.set_yticks(positions)
|
||||
ax.set_yticklabels(characters)
|
||||
ax.invert_yaxis()
|
||||
@@ -40,6 +56,23 @@ def plot_minifigs_per_character(counts_path: Path, destination_path: Path) -> No
|
||||
for index, bar in enumerate(bars):
|
||||
value = counts[index]
|
||||
ax.text(value + 0.1, bar.get_y() + bar.get_height() / 2, str(value), va="center", fontsize=8)
|
||||
legend_entries = []
|
||||
seen = set()
|
||||
for gender in genders:
|
||||
normalized = gender.strip().lower()
|
||||
if normalized in seen:
|
||||
continue
|
||||
seen.add(normalized)
|
||||
legend_entries.append(
|
||||
Patch(
|
||||
facecolor=GENDER_COLORS.get(normalized, GENDER_COLORS["unknown"]),
|
||||
edgecolor="#0d0d0d",
|
||||
linewidth=0.6,
|
||||
label=GENDER_LABELS.get(normalized, "Inconnu"),
|
||||
)
|
||||
)
|
||||
if legend_entries:
|
||||
ax.legend(handles=legend_entries, title="Genre", loc="lower right")
|
||||
|
||||
ensure_parent_dir(destination_path)
|
||||
fig.tight_layout()
|
||||
|
||||
Reference in New Issue
Block a user