1

Compare la répartition des genres minifigs vs personnages

This commit is contained in:
2025-12-03 22:55:05 +01:00
parent cc613a88af
commit a976b57afe
6 changed files with 142 additions and 7 deletions

View File

@@ -25,12 +25,7 @@ def plot_minifig_gender_share(counts_path: Path, destination_path: Path) -> None
genders = [row["gender"] for row in rows]
counts = [int(row["minifig_count"]) for row in rows]
colors = [GENDER_COLORS.get(gender.strip().lower(), GENDER_COLORS["unknown"]) for gender in genders]
total = sum(counts)
labels = []
for gender, count in zip(genders, counts):
percent = (count / total) * 100 if total else 0
label = f"{GENDER_LABELS.get(gender.strip().lower(), 'Inconnu')} ({percent:.1f} %)"
labels.append(label)
labels = [f"{GENDER_LABELS.get(g.strip().lower(), 'Inconnu')} ({count})" for g, count in zip(genders, counts)]
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(
@@ -48,3 +43,31 @@ def plot_minifig_gender_share(counts_path: Path, destination_path: Path) -> None
fig.tight_layout()
fig.savefig(destination_path, dpi=160)
plt.close(fig)
def plot_character_gender_share(counts_path: Path, destination_path: Path) -> None:
"""Trace un diagramme circulaire de la répartition des personnages par genre."""
rows = [row for row in load_gender_counts(counts_path) if row["gender"].strip().lower() in ("male", "female")]
if not rows:
return
genders = [row["gender"] for row in rows]
counts = [int(row["character_count"]) for row in rows]
colors = [GENDER_COLORS.get(gender.strip().lower(), GENDER_COLORS["unknown"]) for gender in genders]
labels = [f"{GENDER_LABELS.get(g.strip().lower(), 'Inconnu')} ({count})" for g, count in zip(genders, counts)]
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(
counts,
labels=labels,
colors=colors,
startangle=90,
wedgeprops={"linewidth": 0.6, "edgecolor": "#0d0d0d"},
)
centre_circle = plt.Circle((0, 0), 0.5, fc="white")
ax.add_artist(centre_circle)
ax.set_title("Répartition des personnages par genre (hors inconnus)")
ensure_parent_dir(destination_path)
fig.tight_layout()
fig.savefig(destination_path, dpi=160)
plt.close(fig)

View File

@@ -195,6 +195,28 @@ def aggregate_by_gender(rows: Iterable[dict]) -> List[dict]:
return aggregates
def aggregate_characters_by_gender(rows: Iterable[dict]) -> List[dict]:
"""Compte les personnages distincts par genre (hors genres inconnus)."""
gender_by_character: Dict[str, str] = {}
counts: Dict[str, int] = defaultdict(int)
for row in rows:
character = row["known_character"].strip()
gender = row.get("gender", "").strip().lower()
if character == "":
continue
if gender not in ("male", "female"):
continue
if character in gender_by_character:
continue
gender_by_character[character] = gender
counts[gender] += 1
aggregates: List[dict] = []
for gender in ("female", "male"):
if gender in counts:
aggregates.append({"gender": gender, "character_count": str(counts[gender])})
return aggregates
def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes par personnage."""
ensure_parent_dir(path)
@@ -206,6 +228,17 @@ def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
writer.writerow(row)
def write_character_gender_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes de personnages par genre."""
ensure_parent_dir(path)
fieldnames = ["gender", "character_count"]
with path.open("w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
def write_new_characters_by_year(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des personnages introduits chaque année."""
ensure_parent_dir(path)