{"spec_id":"dot-matrix-proportional","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ndot-matrix-proportional: Dot Matrix Chart for Proportional Counts\nLibrary: matplotlib 3.11.2 | Python 3.14.10\nQuality: 82/100 | Updated: 2026-11-19\n\"\"\"\n\nimport os\n\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\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 = \"Preferred work model\"  # role: category\nCOUNT = \"Employees\"  # role: count\nTOTAL = \"Surveyed\"  # role: total\n\n# Data: remote work preference survey, replace with your own table, for example df = pd.read_csv(\"survey.csv\")\ndf = pd.DataFrame({CATEGORY: [\"Hybrid\", \"Fully Remote\", \"In-Office\"], COUNT: [44, 35, 21], TOTAL: [100] * 3})\n\n# Plot: reads only df and the constants above\ncounts = df[COUNT].fillna(0).round().astype(int).to_numpy()\ntotal = int(df[TOTAL].max())\nrest = max(total - int(counts.sum()), 0)  # units in no listed category: neutral remainder\nn_cols = int(np.ceil(np.sqrt(total)))\nn_rows = int(np.ceil(total / n_cols))\n\n# One dot per unit, filled left to right and top to bottom\ndot_cats = np.repeat(np.arange(len(df) + 1), np.append(counts, rest))[:total]\nidx = np.arange(total)\nxs = idx % n_cols\nys = n_rows - 1 - idx // n_cols\ndot_colors = [IMPRINT_PALETTE[c % len(IMPRINT_PALETTE)] if c < len(df) else INK_MUTED for c in dot_cats]\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\nfig.subplots_adjust(left=0.2, right=0.8, top=0.84, bottom=0.2)\n\nax.scatter(xs, ys, c=dot_colors, s=190, edgecolors=PAGE_BG, linewidth=0.8, zorder=2)\nax.set_xlim(-0.65, n_cols - 0.35)\nax.set_ylim(-0.65, n_rows - 0.35)\nax.set_aspect(\"equal\")\nax.axis(\"off\")\n\n# Legend: one item per category with its count, centred below the grid\nhandles = [\n    mpatches.Patch(color=IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)], label=f\"{cat}  —  {n} / {total}\")\n    for i, (cat, n) in enumerate(zip(df[CATEGORY], counts, strict=True))\n]\nif rest:\n    handles.append(mpatches.Patch(color=INK_MUTED, label=f\"Other  —  {rest} / {total}\"))\nleg = ax.legend(\n    handles=handles,\n    loc=\"upper center\",\n    bbox_to_anchor=(0.5, -0.04),\n    ncol=len(handles),\n    fontsize=9,\n    frameon=True,\n    handlelength=1.6,\n    handleheight=1.2,\n    borderpad=0.7,\n    columnspacing=1.6,\n)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Style\ntitle = \"Remote Work Survey  ·  dot-matrix-proportional  ·  python  ·  matplotlib  ·  anyplot.ai\"\ntitle_fontsize = max(8, round(12 * 67 / len(title))) if len(title) > 67 else 12\nfig.suptitle(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK, y=0.94)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}