{"spec_id":"treemap-basic","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\ntreemap-basic: Basic Treemap\nLibrary: pygal 3.1.3 | Python 3.13.14\nQuality: 93/100 | Updated: 2026-08-04\n\"\"\"\n\nimport os\n\nimport pygal\nfrom pygal.style import Style\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette (first series always brand green)\nIMPRINT = (\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# Data - Market capitalization by sector and company (in $B)\ndata = {\n    \"Technology\": [\n        {\"value\": 3400, \"label\": \"Apple\"},\n        {\"value\": 3100, \"label\": \"Microsoft\"},\n        {\"value\": 2900, \"label\": \"Nvidia\"},\n    ],\n    \"Financials\": [\n        {\"value\": 950, \"label\": \"Berkshire Hathaway\"},\n        {\"value\": 610, \"label\": \"JPMorgan Chase\"},\n        {\"value\": 560, \"label\": \"Visa\"},\n    ],\n    \"Healthcare\": [\n        {\"value\": 820, \"label\": \"Eli Lilly\"},\n        {\"value\": 480, \"label\": \"UnitedHealth\"},\n        {\"value\": 430, \"label\": \"Johnson & Johnson\"},\n    ],\n    \"Energy\": [\n        {\"value\": 470, \"label\": \"ExxonMobil\"},\n        {\"value\": 300, \"label\": \"Chevron\"},\n        {\"value\": 220, \"label\": \"Shell\"},\n    ],\n}\n\n\ndef _lighten(hex_color, t):\n    \"\"\"Blend hex_color toward white by fraction t (theme-independent).\"\"\"\n    r, g, b = (int(hex_color[i : i + 2], 16) for i in (1, 3, 5))\n    return \"#{:02X}{:02X}{:02X}\".format(*(int(round(c + (255 - c) * t)) for c in (r, g, b)))\n\n\n# Value-proportional shading within each sector: the largest company keeps\n# the full sector color, smaller ones lighten toward white, reinforcing the\n# size hierarchy beyond flat category coloring (spec calls for \"nesting\n# depth or color shading intensity\"). Applied identically in both themes so\n# data colors stay theme-independent.\nfor sector_index, items in enumerate(data.values()):\n    sector_color = IMPRINT[sector_index]\n    max_value = max(item[\"value\"] for item in items)\n    for item in items:\n        item[\"color\"] = _lighten(sector_color, 0.35 * (1 - item[\"value\"] / max_value))\n\n# Custom style for 3200x1800 px canvas with theme-adaptive colors\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=IMPRINT,\n    title_font_size=66,\n    label_font_size=56,\n    major_label_font_size=44,\n    legend_font_size=44,\n    value_font_size=36,\n    # Treemap per-cell captions are CSS class text.label, styled by\n    # value_label_font_size (not label_font_size, which only affects axis\n    # text and has no effect on treemaps).\n    value_label_font_size=20,\n    stroke_width=2.5,\n)\n\n# Create treemap\nchart = pygal.Treemap(\n    width=3200,\n    height=1800,\n    style=custom_style,\n    title=\"Market Capitalization by Sector · treemap-basic · pygal · anyplot.ai\",\n    legend_at_bottom=True,\n    legend_at_bottom_columns=4,\n    print_values=True,\n    print_labels=True,\n    value_formatter=lambda x: f\"${x}B\",\n)\n\n# Add data by sector\nfor sector, items in data.items():\n    chart.add(sector, items)\n\n# Save as PNG and HTML with theme suffix\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}