{"spec_id":"marimekko-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nmarimekko-basic: Basic Marimekko Chart\nLibrary: matplotlib 3.11.1 | Python 3.13.14\nQuality: 92/100 | Updated: 2026-07-24\n\"\"\"\n\nimport os\n\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Data: Market share by region (x-category) and product line (y-category)\nregions = [\"North America\", \"Europe\", \"Asia Pacific\", \"Latin America\"]\nproducts = [\"Electronics\", \"Apparel\", \"Home & Garden\", \"Sports\"]\n\n# Values matrix: rows = products, columns = regions\n# Each column total determines that region's bar width\nvalues = np.array(\n    [\n        [120, 85, 200, 35],  # Electronics\n        [80, 110, 150, 45],  # Apparel\n        [60, 70, 80, 25],  # Home & Garden\n        [40, 35, 70, 15],  # Sports\n    ]\n)\n\n# Imprint palette (positions 1-4 in canonical order)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n# Per-segment label contrast, chosen by contrast ratio against each fill (WCAG large-text >=3:1):\n# green/blue are dark enough for white text; lavender/ochre read better with dark ink text.\nlabel_colors = [\"white\", INK, \"white\", INK]\n\n# Calculate bar widths (proportional to column totals)\ncolumn_totals = values.sum(axis=0)\ntotal = column_totals.sum()\nbar_widths = column_totals / total\ncum_widths = np.concatenate([[0], np.cumsum(bar_widths)[:-1]])\n\n# Focal region: the dominant market by total revenue\nfocal_idx = int(np.argmax(column_totals))\nfocal_share = column_totals[focal_idx] / total * 100\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Subtle highlight band behind the focal region to establish visual hierarchy\nax.axvspan(cum_widths[focal_idx], cum_widths[focal_idx] + bar_widths[focal_idx], color=INK, alpha=0.05, zorder=0)\n\n# Cream halo crisps up dark ink labels on the lighter fills; white labels on the\n# dark/saturated fills already clear WCAG large-text contrast without a stroke,\n# and a light-on-light halo there would blur rather than help.\nink_stroke = [pe.withStroke(linewidth=2.5, foreground=PAGE_BG)]\n\nfor i, (product, color, label_color) in enumerate(zip(products, IMPRINT, label_colors, strict=True)):\n    heights = values[i] / column_totals\n    bottoms = values[:i].sum(axis=0) / column_totals if i > 0 else np.zeros(len(regions))\n\n    for j in range(len(regions)):\n        ax.bar(\n            cum_widths[j] + bar_widths[j] / 2,\n            heights[j],\n            width=bar_widths[j] * 0.98,\n            bottom=bottoms[j],\n            color=color,\n            edgecolor=PAGE_BG,\n            linewidth=1.5,\n            label=product if j == 0 else None,\n            zorder=2,\n        )\n\n        if heights[j] > 0.12:\n            ax.text(\n                cum_widths[j] + bar_widths[j] / 2,\n                bottoms[j] + heights[j] / 2,\n                f\"${values[i, j]}M\",\n                ha=\"center\",\n                va=\"center\",\n                fontsize=9,\n                fontweight=\"bold\",\n                color=label_color,\n                path_effects=ink_stroke if label_color == INK else None,\n                zorder=3,\n            )\n\n# Region labels below bars\nfor j, region in enumerate(regions):\n    ax.text(\n        cum_widths[j] + bar_widths[j] / 2,\n        -0.06,\n        f\"{region}\\n(${column_totals[j]:.0f}M)\",\n        ha=\"center\",\n        va=\"top\",\n        fontsize=10,\n        fontweight=\"bold\",\n        color=INK,\n    )\n\n# Callout: emphasize the dominant region's revenue share (the key insight)\nax.annotate(\n    f\"{regions[focal_idx]} leads at ${column_totals[focal_idx]:.0f}M\\n({focal_share:.0f}% of total revenue)\",\n    xy=(cum_widths[focal_idx] + bar_widths[focal_idx] / 2, 1.0),\n    xytext=(cum_widths[focal_idx] + bar_widths[focal_idx] / 2, 1.24),\n    ha=\"center\",\n    va=\"bottom\",\n    fontsize=9,\n    fontweight=\"bold\",\n    color=INK,\n    arrowprops={\"arrowstyle\": \"-|>\", \"color\": INK_SOFT, \"lw\": 1.2},\n    bbox={\"boxstyle\": \"round,pad=0.4\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9},\n    zorder=4,\n)\n\n# Style\nax.set_xlim(0, 1)\nax.set_ylim(-0.24, 1.42)\nax.set_ylabel(\"Share within Region\", fontsize=10, color=INK)\n\ntitle = \"marimekko-basic · python · matplotlib · anyplot.ai\"\nfig.suptitle(title, fontsize=12, fontweight=\"medium\", color=INK, y=0.99)\nax.set_title(\"Bar width = regional revenue total · Segment height = product share\", fontsize=9, color=INK_MUTED, pad=8)\n\nax.set_yticks([0, 0.25, 0.5, 0.75, 1.0])\nax.set_yticklabels([\"0%\", \"25%\", \"50%\", \"75%\", \"100%\"])\nax.tick_params(axis=\"y\", labelsize=8, colors=INK_SOFT)\nax.set_xticks([])\n\n# Legend below the chart, horizontal layout\nleg = ax.legend(\n    loc=\"upper center\",\n    bbox_to_anchor=(0.5, -0.14),\n    ncol=len(products),\n    fontsize=8,\n    title=\"Product Lines\",\n    title_fontsize=8,\n    frameon=True,\n)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\nleg.get_title().set_color(INK_SOFT)\n\n# Grid and spines\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"bottom\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\n\nfig.subplots_adjust(left=0.08, right=0.97, top=0.86, bottom=0.20)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}