{"spec_id":"dot-matrix-proportional","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ndot-matrix-proportional: Dot Matrix Chart for Proportional Counts\nLibrary: plotly 7.1.0 | Python 3.14.10\nQuality: 88/100 | Updated: 2026-11-18\n\"\"\"\n\nimport math\nimport os\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette: all eight positions, one per category in order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Columns: set these to your table's column names\nCATEGORY = \"Commute mode\"  # role: category\nCOUNT = \"Respondents\"  # role: count\nTOTAL = \"Survey size\"  # role: total\n\n# Data: replace this block with your own table, for example df = pd.read_csv(\"survey.csv\")\ndf = pd.DataFrame(\n    {CATEGORY: [\"Public Transit\", \"Car\", \"Cycling\", \"Work from Home\"], COUNT: [42, 31, 15, 12], TOTAL: [100] * 4}\n)\n\n# Plot\ntotal = int(df[TOTAL].iloc[0])\ngrid_cols = math.ceil(math.sqrt(total))\ngrid_rows = math.ceil(total / grid_cols)\n\n# Fill left-to-right, top-to-bottom: dot i sits in column i % cols, row i // cols (row 0 at the top)\ndot_index = 0\nfig = go.Figure()\nfor i, (cat, count) in enumerate(zip(df[CATEGORY], df[COUNT], strict=True)):\n    slots = range(dot_index, dot_index + int(count))\n    dot_index += int(count)\n    pct = count / total * 100\n    fig.add_trace(\n        go.Scatter(\n            x=[k % grid_cols for k in slots],\n            y=[grid_rows - 1 - k // grid_cols for k in slots],\n            mode=\"markers\",\n            name=f\"{cat}  ·  {count} ({pct:.0f}%)\",\n            marker={\"color\": IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)], \"size\": 22, \"symbol\": \"circle\"},\n            hovertemplate=f\"<b>{cat}</b><br>Count: {count}<br>Share: {pct:.0f}%<extra></extra>\",\n        )\n    )\n\ntop = df.loc[df[COUNT].idxmax()]\ntitle = \"City Commute Survey · dot-matrix-proportional · python · plotly · anyplot.ai\"\n\nfig.update_layout(\n    autosize=False,\n    title={\n        \"text\": title,\n        \"font\": {\"size\": round(16 * min(1.0, 67 / len(title))), \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n        \"y\": 0.975,\n        \"yanchor\": \"top\",\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    xaxis={\"visible\": False, \"range\": [-0.7, grid_cols - 0.3], \"fixedrange\": True},\n    yaxis={\"visible\": False, \"range\": [-0.7, grid_rows - 0.3], \"scaleanchor\": \"x\", \"scaleratio\": 1, \"fixedrange\": True},\n    legend={\n        \"orientation\": \"h\",\n        \"x\": 0.5,\n        \"y\": -0.04,\n        \"xanchor\": \"center\",\n        \"yanchor\": \"top\",\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n        \"font\": {\"color\": INK_SOFT, \"size\": 10},\n        \"tracegroupgap\": 0,\n    },\n    annotations=[\n        # Insight subtitle: draw attention to the largest category\n        {\n            \"text\": f\"<b>{top[COUNT] / total:.0%}</b> · {top[CATEGORY]} is the largest group\",\n            \"x\": 0.5,\n            \"y\": 1.0,\n            \"xref\": \"paper\",\n            \"yref\": \"paper\",\n            \"showarrow\": False,\n            \"font\": {\"size\": 11, \"color\": IMPRINT_PALETTE[0]},\n            \"xanchor\": \"center\",\n            \"yanchor\": \"bottom\",\n        },\n        # Scale context\n        {\n            \"text\": f\"Each ● represents 1 respondent  ·  n = {total}\",\n            \"x\": 0.5,\n            \"y\": -0.005,\n            \"xref\": \"paper\",\n            \"yref\": \"paper\",\n            \"showarrow\": False,\n            \"font\": {\"size\": 9, \"color\": INK_MUTED},\n            \"xanchor\": \"center\",\n            \"yanchor\": \"bottom\",\n        },\n    ],\n    margin={\"l\": 40, \"r\": 40, \"t\": 85, \"b\": 100},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=600, height=600, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}