{"spec_id":"bubble-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbubble-basic: Basic Bubble Chart\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\"\n\n# anyplot palette — canonical order, first series always #009E73\nANYPLOT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Data — Countries: healthcare spending vs child mortality, bubble = population\nnp.random.seed(42)\n\ntier_params = [\n    (\"Low Income\", 50, 300, 85, 22, 12),\n    (\"Lower-Middle Income\", 300, 950, 38, 11, 12),\n    (\"Upper-Middle Income\", 950, 3200, 14, 5, 12),\n    (\"High Income\", 3200, 8500, 5, 2, 12),\n]\ntier_colors = {t[0]: ANYPLOT_PALETTE[i] for i, t in enumerate(tier_params)}\n\nrows = []\nfor tier, s_min, s_max, m_ctr, m_std, n in tier_params:\n    spending = np.random.uniform(s_min, s_max, n)\n    mortality = np.clip(np.random.normal(m_ctr, m_std, n), 0.5, 150)\n    population = np.clip(np.random.lognormal(1.8, 1.1, n), 1.0, 200.0)\n    for s, m, pop in zip(spending, mortality, population, strict=False):\n        rows.append(\n            {\n                \"Healthcare Spending ($/year)\": round(s, 0),\n                \"Child Mortality (per 1,000 births)\": round(m, 1),\n                \"Population (M)\": round(pop, 1),\n                \"Income Tier\": tier,\n            }\n        )\ndf = pd.DataFrame(rows)\n\n# Configure seaborn theme\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nhue_order = [t[0] for t in tier_params]\nsns.scatterplot(\n    data=df,\n    x=\"Healthcare Spending ($/year)\",\n    y=\"Child Mortality (per 1,000 births)\",\n    size=\"Population (M)\",\n    hue=\"Income Tier\",\n    hue_order=hue_order,\n    sizes=(50, 1200),\n    alpha=0.72,\n    palette=tier_colors,\n    edgecolor=PAGE_BG,\n    linewidth=0.7,\n    legend=\"brief\",\n    ax=ax,\n)\n\n# Style\ntitle = \"bubble-basic · python · seaborn · anyplot.ai\"\nn_chars = len(title)\nratio = 67 / n_chars if n_chars > 67 else 1.0\ntitle_fontsize = max(8, round(12 * ratio))\n\nax.set_xlabel(\"Healthcare Spending ($ / year)\", fontsize=10, color=INK)\nax.set_ylabel(\"Child Mortality (per 1,000 births)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8)\nax.xaxis.grid(True, alpha=0.08, linewidth=0.5)\n\n# Move legend first, then configure the resulting object to avoid losing changes\nsns.move_legend(ax, \"upper right\", frameon=True)\nlegend = ax.get_legend()\nlegend.set_title(\"Income Tier / Population (M)\", prop={\"size\": 8})\nlegend.get_title().set_fontweight(\"semibold\")\nlegend.get_title().set_color(INK)\ntier_names = {t[0] for t in tier_params}\nfor handle, text_obj in zip(legend.legend_handles, legend.texts, strict=False):\n    text_obj.set_fontsize(8)\n    text_obj.set_color(INK)\n    # Size handles (non-tier labels): override colors so they're visible in dark mode\n    if text_obj.get_text() not in tier_names:\n        try:\n            handle.set_facecolor(INK_SOFT)\n            handle.set_edgecolor(INK_SOFT)\n        except AttributeError:\n            pass\nlegend.set_frame_on(True)\nlegend.get_frame().set_alpha(0.92)\nlegend.get_frame().set_edgecolor(INK_SOFT)\nlegend.get_frame().set_facecolor(ELEVATED_BG)\n\n# Save — no bbox_inches='tight' to preserve exact 3200×1800 canvas\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}