{"spec_id":"pie-basic","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\npie-basic: Basic Pie Chart\nLibrary: letsplot 4.10.1 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\n\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    element_rect,\n    element_text,\n    geom_pie,\n    ggplot,\n    ggsave,\n    ggsize,\n    labs,\n    layer_labels,\n    layer_tooltips,\n    scale_fill_manual,\n    theme,\n    theme_void,\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\"\nANYPLOT_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# anyplot categorical palette — first series always #009E73\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Data - Global smartphone market share (2024)\ndata = {\n    \"company\": [\"Apple\", \"Samsung\", \"Xiaomi\", \"OPPO\", \"Others\"],\n    \"share\": [23.1, 19.4, 13.7, 8.8, 35.0],\n    \"explode\": [0.0, 0.0, 0.0, 0.08, 0.0],\n}\n\n# Colors: anyplot positions 1–4, muted for \"Others\" (rest category)\ncolors = [IMPRINT_PALETTE[0], IMPRINT_PALETTE[1], IMPRINT_PALETTE[2], IMPRINT_PALETTE[3], ANYPLOT_MUTED]\n\n# Title — minimal required format\ntitle = \"pie-basic · python · letsplot · anyplot.ai\"\ntitle_size = 16\n\n# Rich HTML tooltips — letsplot-specific interactivity\npie_tooltips = layer_tooltips().title(\"@company\").format(\"@share\", \"{.1f}%\").line(\"Market share|@share\")\n\n# Plot — square canvas (600×600 × scale=4 = 2400×2400 px)\nplot = (\n    ggplot(data)\n    + geom_pie(\n        aes(slice=\"share\", fill=\"company\", explode=\"explode\"),\n        stat=\"identity\",\n        size=40,\n        hole=0,\n        stroke=1.5,\n        stroke_side=\"both\",\n        color=PAGE_BG,\n        spacer_width=1.0,\n        spacer_color=PAGE_BG,\n        labels=layer_labels().line(\"@{share}\").format(\"share\", \"{.1f}%\").size(11),\n        tooltips=pie_tooltips,\n    )\n    + scale_fill_manual(values=colors)\n    + labs(\n        title=title,\n        subtitle=\"OPPO's 8.8% slice is the smallest — 'Others' dominate at 35%\",\n        fill=\"Brand\",\n        caption=\"Global smartphone market share, 2024\",\n    )\n    + ggsize(600, 600)\n    + theme_void()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        plot_title=element_text(size=title_size, hjust=0.5, face=\"bold\", color=INK),\n        plot_subtitle=element_text(size=11, hjust=0.5, color=INK_SOFT),\n        plot_caption=element_text(size=9, hjust=0.5, color=ANYPLOT_MUTED),\n        legend_title=element_text(size=12, face=\"bold\", color=INK),\n        legend_text=element_text(size=10, color=INK_SOFT),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        plot_margin=[20, 20, 20, 20],\n        legend_position=\"right\",\n    )\n)\n\n# Save\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}