{"spec_id":"choropleth-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nchoropleth-basic: Choropleth Map with Regional Coloring\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\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# Data - Life expectancy by country (years)\nnp.random.seed(42)\n\ncountries_data = {\n    \"country\": [\n        \"CHN\",\n        \"IND\",\n        \"USA\",\n        \"IDN\",\n        \"BRA\",\n        \"PAK\",\n        \"NGA\",\n        \"BGD\",\n        \"RUS\",\n        \"MEX\",\n        \"JPN\",\n        \"EGY\",\n        \"PHL\",\n        \"ETH\",\n        \"VNM\",\n        \"TUR\",\n        \"IRN\",\n        \"DEU\",\n        \"THA\",\n        \"FRA\",\n        \"GBR\",\n        \"TZA\",\n        \"ITA\",\n        \"KEN\",\n        \"COL\",\n        \"ESP\",\n        \"UKR\",\n        \"DZA\",\n        \"IRQ\",\n        \"AFG\",\n        \"CAN\",\n        \"MAR\",\n        \"AUS\",\n        \"PER\",\n        \"ANG\",\n        \"GHA\",\n        \"YEM\",\n        \"NLD\",\n        \"CHE\",\n        \"POL\",\n        \"SWE\",\n        \"BEL\",\n        \"CZE\",\n        \"GRC\",\n        \"PRT\",\n        \"AUT\",\n        \"NOR\",\n        \"DNK\",\n        \"FIN\",\n        \"ISL\",\n    ],\n    \"life_expectancy\": [\n        78.2,\n        70.5,\n        78.9,\n        72.8,\n        76.1,\n        67.3,\n        54.7,\n        72.8,\n        72.5,\n        75.3,\n        84.5,\n        72.6,\n        71.9,\n        67.9,\n        73.8,\n        77.6,\n        77.2,\n        81.3,\n        76.9,\n        82.5,\n        81.5,\n        65.5,\n        82.8,\n        66.3,\n        76.3,\n        83.5,\n        71.4,\n        77.0,\n        70.2,\n        64.8,\n        82.3,\n        77.4,\n        83.2,\n        76.8,\n        60.8,\n        63.1,\n        66.2,\n        81.9,\n        83.6,\n        78.0,\n        84.2,\n        81.8,\n        79.1,\n        81.1,\n        81.7,\n        81.9,\n        84.1,\n        81.2,\n        82.1,\n        82.7,\n    ],\n}\n\ndf = pd.DataFrame(countries_data)\n\n# Create choropleth map with world scope\nfig = px.choropleth(\n    df,\n    locations=\"country\",\n    color=\"life_expectancy\",\n    color_continuous_scale=\"Viridis\",\n    scope=\"world\",\n    hover_data={\"country\": True, \"life_expectancy\": \":.1f\"},\n    labels={\"life_expectancy\": \"Life Expectancy (years)\"},\n)\n\n# Update colorbar for theme adaptation\nfig.update_layout(\n    coloraxis_colorbar=dict(\n        title=dict(text=\"Life Expectancy<br>(years)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        thickness=28,\n        len=0.75,\n        x=1.02,\n        tickcolor=INK_SOFT,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    )\n)\n\n# Update layout for theme-adaptive rendering\nfig.update_layout(\n    title=dict(\n        text=\"choropleth-basic · plotly · anyplot.ai\",\n        font=dict(size=28, color=INK, family=\"sans-serif\"),\n        x=0.5,\n        xanchor=\"center\",\n        y=0.98,\n        yanchor=\"top\",\n    ),\n    geo=dict(\n        scope=\"world\",\n        projection_type=\"natural earth\",\n        showland=True,\n        landcolor=\"rgba(243, 243, 243, 0.5)\" if THEME == \"light\" else \"rgba(50, 50, 48, 0.5)\",\n        showocean=True,\n        oceancolor=\"rgba(204, 229, 255, 0.3)\" if THEME == \"light\" else \"rgba(100, 140, 180, 0.2)\",\n        showcountries=True,\n        countrycolor=INK_SOFT,\n        countrywidth=1.5,\n        showcoastlines=True,\n        coastlinecolor=INK_SOFT,\n        coastlinewidth=1.5,\n        showlakes=True,\n        lakecolor=\"rgba(200, 220, 255, 0.3)\" if THEME == \"light\" else \"rgba(100, 140, 180, 0.2)\",\n        bgcolor=PAGE_BG,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK, family=\"sans-serif\"),\n    margin=dict(l=40, r=140, t=100, b=40),\n    height=900,\n    width=1600,\n)\n\n# Update traces for border visibility\nfig.update_traces(marker_line_width=2.5, marker_line_color=INK_SOFT)\n\n# Save as PNG (4800x2700 px via scale parameter)\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\n\n# Save as interactive HTML\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}