{"spec_id":"pie-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\npie-basic: Basic Pie Chart\nLibrary: seaborn 0.13.2 | Python 3.14.10\nQuality: 82/100 | Updated: 2026-11-27\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\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# Imprint palette, canonical order, first series always #009E73\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"text.color\": INK,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Column constants\nENERGY_SOURCE = \"Energy source\"  # role: category\nSHARE = \"Share of household energy use (%)\"  # role: value\n\n# Data\ndf = pd.DataFrame(\n    {\n        ENERGY_SOURCE: [\"Natural gas\", \"Electricity\", \"Heating oil\", \"Propane\", \"Wood\", \"Solar\"],\n        SHARE: [38, 27, 14, 9, 7, 5],\n    }\n)\n\n# Plot\nn_slices = len(df)\ncolors = sns.color_palette(IMPRINT_PALETTE, n_slices)\nexplode = ((df[SHARE] == df[SHARE].max()) * 0.06).tolist()\n\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nfig.subplots_adjust(top=0.88, bottom=0.14)\n\nwedges, texts, autotexts = ax.pie(\n    df[SHARE],\n    colors=colors,\n    explode=explode,\n    autopct=\"%1.0f%%\",\n    pctdistance=0.72,\n    startangle=90,\n    counterclock=False,\n    wedgeprops={\"edgecolor\": PAGE_BG, \"linewidth\": 2.0},\n)\n\n# Label ink follows each slice's luminance: white on dark fills, near-black on light ones\nfor autotext, color in zip(autotexts, colors, strict=True):\n    red, green, blue = [c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 for c in color]\n    luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue\n    autotext.set_color(\"#FFFFFF\" if luminance < 0.18 else \"#1A1A17\")\n    autotext.set_fontsize(11)\n    autotext.set_fontweight(\"bold\")\n\nax.set_title(\"pie-basic · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"bold\", color=INK, pad=14)\n\nlegend = ax.legend(\n    wedges,\n    df[ENERGY_SOURCE],\n    title=ENERGY_SOURCE,\n    loc=\"lower center\",\n    bbox_to_anchor=(0.5, -0.12),\n    ncol=3,\n    fontsize=9,\n    title_fontsize=9,\n    frameon=True,\n)\nlegend.get_title().set_color(INK)\nfor text in legend.get_texts():\n    text.set_color(INK_SOFT)\n\n# Save — no tight_layout (subplots_adjust sets the margins) and no bbox_inches\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}