zcahjl3's picture
Deploy data-preserving FigMirror code augmentation 10-case page
6a6928b verified
Raw
History Blame Contribute Delete
4.28 kB
from __future__ import annotations
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams.update({
"pdf.fonttype": 42,
"ps.fonttype": 42,
"figure.dpi": 170,
"savefig.dpi": 240,
"font.family": "DejaVu Sans",
"font.size": 8.2,
"axes.titlesize": 9.6,
"axes.labelsize": 8.4,
"xtick.labelsize": 7.2,
"ytick.labelsize": 7.2,
"legend.fontsize": 7.2,
"axes.linewidth": 0.75,
})
COL_INK = "#172033"
COL_MUTED = "#667085"
COL_GRID = "#d9dee7"
COL_BLUE = "#356ca5"
COL_TEAL = "#2f9c95"
COL_ORANGE = "#d8863b"
COL_RED = "#c75756"
COL_PURPLE = "#8066a8"
COL_GREEN = "#5f9b68"
def polish_axes(ax, grid_axis="y"):
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_color("#303642")
ax.spines["bottom"].set_color("#303642")
ax.tick_params(length=0, colors=COL_MUTED, pad=3)
if grid_axis:
ax.grid(True, axis=grid_axis, color=COL_GRID, linewidth=0.55, alpha=0.85)
ax.set_axisbelow(True)
def save(fig):
fig.savefig("augmented.png", bbox_inches="tight", facecolor="white")
fig.savefig("augmented.pdf", bbox_inches="tight", facecolor="white")
import pandas as pd
# DATA SECTOR: same countries, 2024 values, region map, derived avg share, and growth rate as original.py.
countries = ["South Africa", "Thailand", "Venezuela", "Chile", "Argentina", "Nigeria", "India", "Kenya", "Bangladesh (SA)", "Ethiopia", "Ghana", "Uganda", "Mozambique", "Rwanda", "Eritrea", "Tanzania", "Zambia", "Namibia", "Botswana", "Peru", "Colombia"]
base_2024 = [17.1, 57.7, 37.8, 31.3, 25.3, 44.8, 22.9, 28.4, 34.0, 31.4, 27.3, 27.8, 31.4, 32.9, 29.3, 20.8, 25.5, 22.3, 18.8, 30.2, 28.5]
region_map = {"South Africa": "Sub-Saharan Africa", "Nigeria": "Sub-Saharan Africa", "Kenya": "Sub-Saharan Africa", "Ethiopia": "Sub-Saharan Africa", "Ghana": "Sub-Saharan Africa", "Uganda": "Sub-Saharan Africa", "Mozambique": "Sub-Saharan Africa", "Rwanda": "Sub-Saharan Africa", "Eritrea": "Sub-Saharan Africa", "Tanzania": "Sub-Saharan Africa", "Zambia": "Sub-Saharan Africa", "Namibia": "Sub-Saharan Africa", "Botswana": "Sub-Saharan Africa", "India": "South Asia", "Bangladesh (SA)": "South Asia", "Thailand": "South Asia", "Venezuela": "Latin America", "Chile": "Latin America", "Argentina": "Latin America", "Peru": "Latin America", "Colombia": "Latin America"}
records = []
for country, v2024 in zip(countries, base_2024):
v2022 = round(v2024 - 1.5, 1)
v2023 = round(v2024 - 0.5, 1)
avg_share = round((v2022 + v2023 + v2024) / 3, 2)
growth_rate = round((v2024 - v2022) / v2022 * 100, 2)
records.append({"Country": country, "Region": region_map[country], "AvgShare": avg_share, "GrowthRate": growth_rate})
df = pd.DataFrame(records).sort_values("AvgShare", ascending=False)
region_colors = {"Sub-Saharan Africa": COL_BLUE, "South Asia": COL_TEAL, "Latin America": COL_ORANGE}
fig, ax1 = plt.subplots(figsize=(7.6, 3.9))
x = np.arange(len(df))
bar_colors = [region_colors[r] for r in df["Region"]]
bars = ax1.bar(x, df["AvgShare"], color=bar_colors, edgecolor="white", linewidth=0.65, width=0.72, label="Avg share")
ax1.set_ylabel("Avg female employment share (%)", color=COL_BLUE)
ax1.set_xticks(x, df["Country"], rotation=42, ha="right")
ax1.tick_params(axis="y", labelcolor=COL_BLUE)
polish_axes(ax1, "y")
ax2 = ax1.twinx()
ax2.plot(x, df["GrowthRate"], color=COL_RED, marker="o", markersize=3.8, linewidth=1.45, label="Growth rate")
ax2.set_ylabel("Growth rate, 2022-2024 (%)", color=COL_RED)
ax2.tick_params(length=0, colors=COL_RED, pad=3)
ax2.spines["top"].set_visible(False)
ax2.spines["right"].set_color("#303642")
region_handles = [plt.Line2D([0], [0], marker="s", linestyle="", markerfacecolor=c, markeredgecolor="none", markersize=7, label=r) for r, c in region_colors.items()]
line_handle = plt.Line2D([0], [0], color=COL_RED, marker="o", linewidth=1.4, markersize=4, label="Growth rate")
ax1.legend(handles=region_handles + [line_handle], frameon=False, ncol=4, loc="upper left", bbox_to_anchor=(0, 1.10), columnspacing=0.9)
ax1.set_title("Female vulnerable employment share and growth", loc="left", color=COL_INK, fontweight=600, pad=20)
fig.subplots_adjust(left=0.08, right=0.91, bottom=0.33, top=0.83)
save(fig)