{"spec_id":"slope-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nslope-basic: Basic Slope Chart (Slopegraph)\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 93/100 | Updated: 2026-07-26\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_line,\n    geom_point,\n    geom_text,\n    ggplot,\n    labs,\n    scale_color_manual,\n    scale_x_continuous,\n    theme,\n    theme_minimal,\n)\n\n\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# Increase -> Imprint position 1 (also the semantic \"gain\" green);\n# Decrease -> Imprint position 5 (the semantic \"loss\" red).\nCOLOR_INCREASE = \"#009E73\"\nCOLOR_DECREASE = \"#AE3030\"\n\n# Data: national coal share of electricity generation, 2014 vs 2024\n# (approximate figures consistent with IEA/Ember energy-mix statistics).\n# Most advanced economies phased coal out under climate policy while a\n# few emerging economies leaned on it for energy security -- a mix of\n# increases, decreases and rank reversals for a slopegraph to tell.\nentities = [\n    \"Germany\",\n    \"United Kingdom\",\n    \"Poland\",\n    \"South Africa\",\n    \"United States\",\n    \"Australia\",\n    \"India\",\n    \"China\",\n    \"Vietnam\",\n    \"Turkey\",\n]\n# Poland and Vietnam 2024 values nudged slightly further from their nearest\n# neighbor (China, Turkey) so the endpoint markers no longer visually merge.\nshare_2014 = [44, 30, 84, 92, 39, 63, 74, 66, 19, 27]\nshare_2024 = [23, 1, 52, 84, 15, 42, 76, 59, 30, 36]\n\nchanges = [\"Increase\" if end >= start else \"Decrease\" for start, end in zip(share_2014, share_2024, strict=True)]\n\ndf_long = pd.DataFrame(\n    {\n        \"entity\": entities * 2,\n        \"x\": [1] * len(entities) + [2] * len(entities),\n        \"value\": share_2014 + share_2024,\n        \"change\": changes * 2,\n    }\n)\n\ndf_labels_left = pd.DataFrame(\n    {\n        \"entity\": entities,\n        \"x\": [1] * len(entities),\n        \"value\": share_2014,\n        \"change\": changes,\n        \"label\": [f\"{e} ({v})\" for e, v in zip(entities, share_2014, strict=True)],\n    }\n)\n\ndf_labels_right = pd.DataFrame(\n    {\n        \"entity\": entities,\n        \"x\": [2] * len(entities),\n        \"value\": share_2024,\n        \"change\": changes,\n        \"label\": [str(v) for v in share_2024],\n    }\n)\n\nplot = (\n    ggplot(df_long, aes(x=\"x\", y=\"value\", group=\"entity\", color=\"change\"))\n    + geom_line(size=1.4, alpha=0.85)\n    + geom_point(size=3.2)\n    # Left labels: entity name + starting value\n    + geom_text(aes(label=\"label\"), data=df_labels_left, ha=\"right\", nudge_x=-0.06, size=3.2, color=INK)\n    # Right labels: ending value only\n    + geom_text(aes(label=\"label\"), data=df_labels_right, ha=\"left\", nudge_x=0.06, size=3.2, color=INK)\n    + scale_color_manual(values={\"Increase\": COLOR_INCREASE, \"Decrease\": COLOR_DECREASE})\n    + scale_x_continuous(breaks=[1, 2], labels=[\"2014\", \"2024\"], limits=(0.55, 2.3))\n    + labs(\n        x=\"\",\n        y=\"Coal Share of Electricity Generation (%)\",\n        title=\"National Coal Power Share · slope-basic · python · plotnine · anyplot.ai\",\n        color=\"Change\",\n    )\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        text=element_text(size=7, color=INK_SOFT),\n        plot_title=element_text(size=11, color=INK),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        axis_text_x=element_text(size=8, color=INK_SOFT),\n        axis_text_y=element_blank(),\n        axis_ticks=element_blank(),\n        legend_text=element_text(size=8, color=INK_SOFT),\n        legend_title=element_text(size=9, color=INK),\n        legend_position=\"right\",\n        legend_background=element_rect(fill=ELEVATED_BG, color=\"none\"),\n        legend_box_spacing=0.015,\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_border=element_blank(),\n        panel_grid_major_x=element_blank(),\n        panel_grid_minor_x=element_blank(),\n        panel_grid_major_y=element_line(color=INK, size=0.3, alpha=0.15),\n        panel_grid_minor_y=element_blank(),\n    )\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}