{"spec_id":"parliament-basic","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nparliament-basic: Parliament Seat Chart\nLibrary: letsplot 4.11.0 | Python 3.14.10\nQuality: 85/100 | Updated: 2026-12-05\n\"\"\"\n\nimport os\nimport re\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    coord_fixed,\n    element_rect,\n    element_text,\n    geom_point,\n    geom_segment,\n    geom_text,\n    gggrid,\n    ggplot,\n    ggsave,\n    ggsize,\n    labs,\n    scale_color_manual,\n    scale_y_continuous,\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\"\n\n# Imprint palette: all eight positions, one per party in order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Constants\nN_ROWS = 8  # concentric arcs of the chamber\nINNER_RADIUS = 0.45\nROW_GAP = 0.075\n\n# Columns: set these to your table's column names\nPARTY = \"Party\"  # role: party\nSEATS = \"Seats\"  # role: seats\nCOLOR = \"Color\"  # role: color\n\n# Data: replace this block with your own table, for example df = pd.read_csv(\"chamber.csv\")\ndf = pd.DataFrame(\n    {\n        PARTY: [\n            \"Riverbank Alliance\",\n            \"Weavers' Union\",\n            \"Harbor League\",\n            \"Quarry Collective\",\n            \"Orchard Guild\",\n            \"Lantern Assembly\",\n        ],\n        SEATS: [85, 72, 58, 35, 95, 55],\n        COLOR: [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\"],\n    }\n)\n\n# Plot\ntotal = int(df[SEATS].astype(int).sum())\nmajority = total // 2 + 1\n\n# Rows hold seats in proportion to their radius, so seat spacing is even\nradii = INNER_RADIUS + ROW_GAP * np.arange(N_ROWS)\nper_row = np.floor(radii / radii.sum() * total).astype(int)\nper_row[-1] += total - per_row.sum()\n\nangle = np.concatenate([np.linspace(np.pi, 0, n) for n in per_row])\nradius = np.concatenate([np.full(n, r) for n, r in zip(per_row, radii, strict=True)])\n\n# Sweep the seats from left to right and hand them out party by party\norder = np.lexsort((radius, -angle))\nangle, radius = angle[order], radius[order]\nlabels = [f\"{p} ({int(s)})\" for p, s in zip(df[PARTY], df[SEATS], strict=True)]\nseat_party = np.repeat(labels, df[SEATS].astype(int))\n\nseats = pd.DataFrame(\n    {\"x\": radius * np.cos(angle), \"y\": radius * np.sin(angle), \"label\": pd.Categorical(seat_party, categories=labels)}\n)\n\n# A party's own hex color wins; anything else, or a table without the optional color column,\n# falls back to the Imprint palette in order\nown_colors = df[COLOR].tolist() if COLOR in df.columns else [None] * len(df)\nseat_colors = [\n    c if re.fullmatch(r\"#[0-9A-Fa-f]{6}\", str(c)) else IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)]\n    for i, c in enumerate(own_colors)\n]\n\n# Majority threshold: a radial line between the last seat of the majority and the next one,\n# drawn in the gaps inside and outside the seat arcs so it never covers a seat\ncut = (angle[majority - 1] + angle[majority]) / 2\nr_in, r_out = radii[0] - 0.09, radii[-1] + 0.09\nr_label = r_out + 0.17\nthreshold = pd.DataFrame(\n    {\n        \"x\": [r_in * np.cos(cut), r_out * np.cos(cut)],\n        \"y\": [r_in * np.sin(cut), r_out * np.sin(cut)],\n        \"xend\": [(r_in - 0.1) * np.cos(cut), (r_out + 0.07) * np.cos(cut)],\n        \"yend\": [(r_in - 0.1) * np.sin(cut), (r_out + 0.07) * np.sin(cut)],\n    }\n)\nthreshold_label = pd.DataFrame(\n    {\"x\": [r_label * np.cos(cut)], \"y\": [r_label * np.sin(cut)], \"text\": [f\"Majority: {majority} of {total}\"]}\n)\n\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    plot_title=element_text(color=INK, size=16, hjust=0.5),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(color=INK_SOFT, size=11),\n    legend_title=element_text(color=INK, size=12),\n    plot_margin=[24, 30, 0, 30],\n)\n\nplot = (\n    ggplot()\n    + geom_point(aes(x=\"x\", y=\"y\", color=\"label\"), data=seats, size=3.2)\n    + geom_segment(aes(x=\"x\", y=\"y\", xend=\"xend\", yend=\"yend\"), data=threshold, color=INK, size=0.8)\n    + geom_text(aes(x=\"x\", y=\"y\", label=\"text\"), data=threshold_label, color=INK_SOFT, size=5)\n    + scale_color_manual(values=seat_colors, breaks=labels)\n    + scale_y_continuous(expand=[0.105, 0])\n    + coord_fixed()\n    + theme_void()\n    + anyplot_theme\n    + labs(title=\"parliament-basic · python · letsplot · anyplot.ai\", color=f\"{PARTY} (seats)\")\n)\n\n# coord_fixed narrows the plot box; a one-panel grid paints the whole canvas in the page color\nplot = gggrid([plot]) + ggsize(800, 450) + theme(plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG))\n\n# Save as PNG and HTML with theme suffix\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}