{"spec_id":"parliament-basic","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nparliament-basic: Parliament Seat Chart\nLibrary: pygal 3.1.3 | Python 3.14.10\nQuality: 87/100 | Updated: 2026-11-09\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\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_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Imprint palette: all eight positions in canonical order (first series ALWAYS #009E73)\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\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\")\n# An invented chamber: made-up party names (a neutral noun plus a group word) and made-up seat\n# counts, in data order (not a left-right spectrum); no real country, parliament or election\ndf = pd.DataFrame(\n    {\n        PARTY: [\n            \"Harborview Assembly\",\n            \"Rivermouth Coalition\",\n            \"Timberwright Union\",\n            \"Stonebridge Front\",\n            \"Lowland Alliance\",\n            \"Millbrook Guild\",\n            \"Ashford Collective\",\n        ],\n        SEATS: [158, 122, 95, 70, 54, 38, 12],\n        COLOR: [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"],\n    }\n)\n\n# Plot: concentric arcs whose seat capacity grows with their radius; all seats are sorted by angle\n# from the left end (180°) to the right end (0°), and the parties claim them in data order\nparties = df.dropna(subset=[PARTY, SEATS]).reset_index(drop=True)\nseat_counts = parties[SEATS].astype(int).clip(lower=0).tolist()\n# one dot per seat; a chamber far beyond the spec's 700 seats gets one dot per block of seats instead\nseats_per_dot = max(1, -(-sum(seat_counts) // 1000))\ndot_counts = [round(seats / seats_per_dot) for seats in seat_counts]\ntotal = sum(dot_counts)\nn_rows = max(1, round(np.sqrt(total / 3)))  # rows about as far apart as neighbouring seats in a row\nradii = np.linspace(0.32, 1.0, n_rows) if n_rows > 1 else np.array([1.0])\nraw = total * radii / radii.sum()\ncapacity = np.floor(raw).astype(int)\nfor row in np.argsort(capacity - raw)[: total - capacity.sum()]:  # largest remainders take the leftover seats\n    capacity[row] += 1\nslots = sorted(\n    (((j + 0.5) / cap * np.pi, radius) for radius, cap in zip(radii, capacity, strict=True) for j in range(cap)),\n    key=lambda slot: (-slot[0], slot[1]),\n)\nstarts = np.cumsum([0, *dot_counts])\n# the table's own hex colours; a party without one takes the next Imprint colour nobody uses yet,\n# and only past the palette's eight colours does one repeat\ngiven = [\n    color if isinstance(color, str) and color.startswith(\"#\") else None\n    for color in (parties[COLOR] if COLOR in parties else [None] * len(parties))\n]\nspare = iter([color for color in IMPRINT_PALETTE if color not in given])\ncolors = [color or next(spare, IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)]) for i, color in enumerate(given)]\n\n# Equal scale on both axes keeps the half circle round: the plot area is about 2.2 times as wide as tall\ny_range = (-0.06, 1.06)\nx_half = (y_range[1] - y_range[0]) * 2.2 / 2\nseat_pitch = 0.68 / max(n_rows - 1, 1) if n_rows > 1 else np.pi / max(total, 1)\ndot_radius = max(4, round(0.4 * seat_pitch * 1400 / (y_range[1] - y_range[0])))  # in px on a ~1400 px tall plot\n# pygal reserves floor(n / columns) legend rows at the bottom; a last, partly filled row needs its own room\nlegend_columns = 3\nlegend_room = 1.5 * 46 * (-(-len(parties) // legend_columns) - len(parties) // legend_columns)\n\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_SOFT,\n    colors=tuple(colors),\n    title_font_size=66,\n    label_font_size=46,\n    major_label_font_size=46,\n    legend_font_size=46,\n    value_font_size=36,\n    opacity=1,\n    opacity_hover=1,\n)\n\nchart = pygal.XY(\n    width=3200,\n    height=1800,\n    style=custom_style,\n    title=\"parliament-basic · python · pygal · anyplot.ai\",\n    x_title=f\"Each dot stands for {seats_per_dot:,} seats\" if seats_per_dot > 1 else None,\n    show_legend=True,\n    legend_at_bottom=True,\n    legend_at_bottom_columns=legend_columns,\n    legend_box_size=46,\n    truncate_legend=-1,\n    show_dots=True,\n    stroke=False,\n    show_x_guides=False,\n    show_y_guides=False,\n    show_x_labels=False,\n    show_y_labels=False,\n    xrange=(-x_half, x_half),\n    range=y_range,\n    spacing=24,\n    margin=40,\n    margin_bottom=40 + legend_room,\n)\n\nfor i, party in enumerate(parties[PARTY]):\n    party_slots = slots[starts[i] : starts[i + 1]]\n    points = [(radius * np.cos(angle), radius * np.sin(angle)) for angle, radius in party_slots]\n    chart.add(f\"{party} ({seat_counts[i]})\", points, dots_size=dot_radius)\n\n# Save\nchart.render_to_file(f\"plot-{THEME}.html\")\nchart.render_to_png(f\"plot-{THEME}.png\")\n"}