{"spec_id":"parliament-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nparliament-basic: Parliament Seat Chart\nLibrary: plotly 7.1.0 | Python 3.14.10\nQuality: 84/100 | Updated: 2026-12-03\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport plotly.graph_objects as go\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 is always #009E73)\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Constants\nN_ROWS = 12  # concentric arcs\nINNER_RADIUS = 0.35\nROW_SPACING = 0.055\nMARKER_SIZE = 11\n\n# Column constants\nPARTY = \"party\"  # role: party\nSEATS = \"seats\"  # role: seats\n\n# Data - fictional chamber with made-up party names\ndf = pd.DataFrame(\n    {\n        PARTY: [\n            \"Meadow Alliance\",\n            \"Civic Union\",\n            \"Harbor Union\",\n            \"Lantern Party\",\n            \"Reform Coalition\",\n            \"Independent Group\",\n        ],\n        SEATS: [145, 118, 52, 48, 35, 22],\n    }\n)\n\n# Plot\nseats = df[SEATS].fillna(0).round().astype(int).to_numpy()\ntotal_seats = int(seats.sum())\nmajority_threshold = total_seats // 2 + 1\n\n# Seats per arc, proportional to the arc's radius, adjusted to the exact total\nradii = INNER_RADIUS + ROW_SPACING * np.arange(N_ROWS)\nrow_seats = np.floor(total_seats * radii / radii.sum()).astype(int)\nfor i in range(total_seats - row_seats.sum()):\n    row_seats[N_ROWS - 1 - i % N_ROWS] += 1\n\n# Seat layout: angle runs from left (pi) to right (0) along each arc\nseat_x, seat_y, seat_angle = [], [], []\nfor radius, n in zip(radii, row_seats, strict=True):\n    angle = np.pi - (np.arange(n) + 0.5) * np.pi / n\n    seat_x.extend(radius * np.cos(angle))\n    seat_y.extend(radius * np.sin(angle))\n    seat_angle.extend(angle)\norder = np.argsort(-np.array(seat_angle), kind=\"stable\")\nseat_x = np.array(seat_x)[order]\nseat_y = np.array(seat_y)[order]\nseat_angle = np.array(seat_angle)[order]\n\n# Parties take consecutive blocks of seats from left to right\nends = seats.cumsum()\nstarts = ends - seats\n\nfig = go.Figure()\nfor i, (party, n) in enumerate(zip(df[PARTY], seats, strict=True)):\n    sl = slice(starts[i], ends[i])\n    fig.add_trace(\n        go.Scatter(\n            x=seat_x[sl],\n            y=seat_y[sl],\n            mode=\"markers\",\n            marker={\"size\": MARKER_SIZE, \"color\": IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)]},\n            name=f\"{party} ({n})\",\n            hovertemplate=f\"{party}<br>Seats: {n}<extra></extra>\",\n        )\n    )\n\n# Majority threshold: radial tick outside the arcs, clear of seats, between the last seat of the majority and the next one\ncut = 0.5 * (seat_angle[majority_threshold - 1] + seat_angle[majority_threshold])\nr_in, r_out = radii[-1] + 0.05, radii[-1] + 0.14\nfig.add_trace(\n    go.Scatter(\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        mode=\"lines\",\n        line={\"color\": INK_SOFT, \"width\": 2, \"dash\": \"dash\"},\n        name=f\"Majority line ({majority_threshold})\",\n        hoverinfo=\"skip\",\n    )\n)\nfig.add_annotation(\n    x=(r_out + 0.03) * np.cos(cut),\n    y=(r_out + 0.03) * np.sin(cut),\n    text=f\"Majority: {majority_threshold}\",\n    showarrow=False,\n    yanchor=\"bottom\",\n    font={\"size\": 12, \"color\": INK_SOFT},\n)\nfig.add_annotation(\n    x=0, y=0.08, text=f\"<b>{total_seats}</b><br>seats\", font={\"size\": 18, \"color\": INK}, showarrow=False, align=\"center\"\n)\n\nfig.update_layout(\n    autosize=False,\n    width=800,\n    height=450,\n    title={\n        \"text\": \"parliament-basic · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 20, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"showgrid\": False,\n        \"zeroline\": False,\n        \"showticklabels\": False,\n        \"range\": [-1.2, 1.2],\n        \"scaleanchor\": \"y\",\n        \"scaleratio\": 1,\n    },\n    yaxis={\"showgrid\": False, \"zeroline\": False, \"showticklabels\": False, \"range\": [-0.1, 1.2]},\n    legend={\n        \"orientation\": \"h\",\n        \"yanchor\": \"top\",\n        \"y\": -0.02,\n        \"xanchor\": \"center\",\n        \"x\": 0.5,\n        \"font\": {\"size\": 11, \"color\": INK_SOFT},\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n        \"itemsizing\": \"constant\",\n    },\n    font={\"color\": INK},\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    margin={\"l\": 30, \"r\": 30, \"t\": 60, \"b\": 70},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}