{"spec_id":"pictogram-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\npictogram-basic: Pictogram Chart (Isotype Visualization)\nLibrary: matplotlib 3.11.2 | Python 3.14.10\nQuality: 84/100 | Updated: 2026-11-30\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom matplotlib.lines import Line2D\nfrom matplotlib.patches import Circle, Rectangle\n\n\n# Theme tokens — Imprint palette chrome\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# Imprint palette — position 1 (brand green) is the single series\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\nBRAND = IMPRINT_PALETTE[0]\n\n# Constants\nMAX_ROW_ICONS = 8  # the longest row holds about this many icons\n\n# Columns: set these to your table's column names\nFRUIT = \"Fruit\"  # role: category\nPRODUCTION = \"Production (Mt)\"  # role: value\n\n# Data: replace this block with your own table, for example df = pd.read_csv(\"production.csv\")\ndf = pd.DataFrame({FRUIT: [\"Apples\", \"Oranges\", \"Bananas\", \"Grapes\", \"Mangoes\"], PRODUCTION: [93, 76, 135, 79, 58]})\n\n# Plot\ndf = df.dropna(subset=[PRODUCTION])\ncategories = df[FRUIT].tolist()\nvalues = df[PRODUCTION].astype(float).tolist()\n# One icon = a round unit (1, 2 or 5 times a power of ten) that fits the largest value in about MAX_ROW_ICONS icons\nraw_unit = max(values) / MAX_ROW_ICONS\nmagnitude = 10 ** np.floor(np.log10(raw_unit))\nICON_UNIT = max(m * magnitude for m in (1, 2, 5, 10) if m * magnitude <= raw_unit * 1.0001)\nn_cats = len(categories)\nfull_icons = [int(v // ICON_UNIT) for v in values]\npartials = [(v % ICON_UNIT) / ICON_UNIT for v in values]\nmax_icons = max(full_icons) + 1  # longest row incl. its partial icon\n\n# Layout parameters\nicon_r = 0.33  # icon radius in data units\nx_step = 0.88  # center-to-center horizontal spacing\nROW_GAP = 1.6  # vertical spacing between rows, so the rows fill the equal-aspect canvas\ny_rows = [ROW_GAP * i for i in range(n_cats - 1, -1, -1)]  # top → bottom\n\n# Figure\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Draw icons row by row\nfor row_idx, (n_full, frac) in enumerate(zip(full_icons, partials, strict=True)):\n    y = y_rows[row_idx]\n\n    # Full filled icons\n    for i in range(n_full):\n        ax.add_patch(\n            Circle((i * x_step, y), icon_r, facecolor=BRAND, edgecolor=\"none\", transform=ax.transData, zorder=3)\n        )\n\n    # Partial icon: muted background + left-filled arc polygon\n    if frac > 0:\n        x_p = n_full * x_step\n        ax.add_patch(\n            Circle(\n                (x_p, y), icon_r, facecolor=INK_MUTED, alpha=0.25, edgecolor=\"none\", transform=ax.transData, zorder=2\n            )\n        )\n        # Brand circle clipped to its left `frac` share\n        filled = Circle((x_p, y), icon_r, facecolor=BRAND, edgecolor=\"none\", zorder=3)\n        ax.add_patch(filled)\n        filled.set_clip_path(\n            Rectangle((x_p - icon_r, y - icon_r), 2 * icon_r * frac, 2 * icon_r, transform=ax.transData)\n        )\n\n    # Value annotation at row end\n    x_end = (n_full + (1 if frac > 0 else 0)) * x_step + icon_r + 0.20\n    ax.text(x_end, y, f\"{values[row_idx]:g} Mt\", fontsize=9, color=INK_SOFT, va=\"center\", ha=\"left\")\n\n# Axes bounds\nax.set_xlim(-icon_r * 2.2, (max_icons - 1) * x_step + icon_r + 1.9)\nax.set_ylim(-0.9 * ROW_GAP, (n_cats - 1) * ROW_GAP + 0.9 * ROW_GAP)\nax.set_aspect(\"equal\", adjustable=\"datalim\")  # keep icons circular whatever the row length\n\n# Category labels on left axis\nax.set_yticks(y_rows)\nax.set_yticklabels(categories, fontsize=10, color=INK_SOFT)\nax.tick_params(axis=\"y\", length=0, labelcolor=INK_SOFT)\nax.set_xticks([])\n\n# Spine styling — L-frame (left + bottom) for row anchoring\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"bottom\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_linewidth(0.5)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"left\"].set_linewidth(0.5)\n\n# Title\ntitle = \"Estimated Global Fruit Production · pictogram-basic · python · matplotlib · anyplot.ai\"\nn_t = len(title)\ntitle_fs = max(8, round(12 * 67 / n_t)) if n_t > 67 else 12\nax.set_title(title, fontsize=title_fs, fontweight=\"medium\", color=INK, pad=12)\n\n# Legend: unit key\nleg = ax.legend(\n    handles=[\n        Line2D(\n            [],\n            [],\n            marker=\"o\",\n            linestyle=\"none\",\n            markersize=9,\n            markerfacecolor=BRAND,\n            markeredgecolor=\"none\",\n            label=f\"= {ICON_UNIT:g} million tonnes\",\n        )\n    ],\n    fontsize=8,\n    loc=\"lower right\",\n    facecolor=ELEVATED_BG,\n    edgecolor=INK_SOFT,\n    framealpha=0.9,\n)\nleg.get_frame().set_linewidth(0.5)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\nfig.subplots_adjust(left=0.13, right=0.97, top=0.91, bottom=0.06)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}