{"spec_id":"bubble-basic","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nbubble-basic: Basic Bubble Chart\nLibrary: letsplot 4.11.0 | Python 3.13.15\nQuality: 87/100 | Updated: 2026-09-30\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_point,\n    ggplot,\n    ggsave,\n    ggsize,\n    ggtb,\n    guide_legend,\n    guides,\n    labs,\n    layer_tooltips,\n    scale_fill_manual,\n    scale_size_area,\n    scale_x_continuous,\n    theme,\n    theme_minimal,\n)\n\n\nLetsPlot.setup_html()\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\"\nRULE = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette — 8 hues, theme-independent, hybrid-v3 sort\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Data - sports analytics: team payroll vs. win rate, bubble size = average attendance\nnp.random.seed(42)\n\nconferences = [\"Eastern\", \"Western\"]\nwin_pct_offset = {\"Eastern\": 0.0, \"Western\": 5.0}\nn_per_conference = 35\n\nrows = []\nfor conference in conferences:\n    payroll_millions = np.random.uniform(90, 190, n_per_conference)\n    win_pct = np.clip(\n        18 + win_pct_offset[conference] + 0.28 * (payroll_millions - 90) + np.random.normal(0, 9, n_per_conference),\n        15,\n        78,\n    )\n    attendance_thousands = np.clip(np.random.normal(28, 7, n_per_conference), 12, 45)\n    for payroll, win, attendance in zip(payroll_millions, win_pct, attendance_thousands, strict=True):\n        rows.append({\"payroll\": payroll, \"win_pct\": win, \"attendance\": attendance, \"conference\": conference})\n\ndf = pd.DataFrame(rows)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"payroll\", y=\"win_pct\", size=\"attendance\", fill=\"conference\"))\n    + geom_point(\n        shape=21,\n        color=INK_SOFT,\n        stroke=0.4,\n        alpha=0.55,\n        tooltips=layer_tooltips()\n        .format(\"payroll\", \"${.0f}M\")\n        .format(\"win_pct\", \"{.1f}%\")\n        .format(\"attendance\", \"{.1f}K\")\n        .line(\"@conference\")\n        .line(\"Payroll|@payroll\")\n        .line(\"Win Rate|@win_pct\")\n        .line(\"Avg. Attendance|@attendance\"),\n    )\n    + scale_size_area(max_size=13, name=\"Avg. Attendance (K)\", breaks=[15, 25, 35, 45])\n    + scale_fill_manual(values=IMPRINT_PALETTE[:2], name=\"Conference\")\n    + scale_x_continuous(expand=[0.02, 5])\n    + guides(\n        fill=guide_legend(nrow=1, override_aes={\"size\": 7}),\n        size=guide_legend(nrow=1, override_aes={\"fill\": IMPRINT_PALETTE[0], \"color\": INK_SOFT, \"alpha\": 0.55}),\n    )\n    + labs(x=\"Team Payroll (Million USD)\", y=\"Win Rate (%)\", title=\"bubble-basic · python · letsplot · anyplot.ai\")\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        # color must match fill explicitly: an unset color on panel_background\n        # renders a visible default border once a legend is present, even\n        # with panel_border=element_blank() (lets-plot 4.11.0 quirk).\n        panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_border=element_blank(),\n        axis_title=element_text(size=13, color=INK),\n        axis_text=element_text(size=10, color=INK_SOFT),\n        plot_title=element_text(size=18, color=INK),\n        plot_margin=[30, 20, 20, 20],\n        legend_title=element_text(size=10, color=INK),\n        legend_text=element_text(size=10, color=INK_SOFT),\n        # color must match fill: legend frames are removed per the\n        # decoration-removal checklist (no visible box border).\n        legend_background=element_rect(fill=ELEVATED_BG, color=ELEVATED_BG),\n        panel_grid_major=element_line(size=0.3, color=RULE),\n        panel_grid_minor=element_blank(),\n        legend_position=\"bottom\",\n        legend_box=\"horizontal\",\n        legend_spacing=20,\n    )\n    + ggsize(800, 450)\n)\n\n# Save: static PNG without the interactive toolbar, HTML with letsplot's\n# distinctive pan/zoom toolbar (ggtb) layered on top for the interactive export\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot + ggtb(), f\"plot-{THEME}.html\", path=\".\")\n"}