{"spec_id":"heatmap-adjacency","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nheatmap-adjacency: Network Adjacency Matrix Heatmap\nLibrary: letsplot 4.11.0 | Python 3.14.10\nQuality: 81/100 | Updated: 2026-11-16\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    element_blank,\n    element_rect,\n    element_text,\n    geom_tile,\n    ggplot,\n    ggsave,\n    ggsize,\n    labs,\n    scale_fill_gradient,\n    theme,\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; imprint_seq runs from position 1 to position 3\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\nSEQ_LOW, SEQ_HIGH = IMPRINT_PALETTE[0], IMPRINT_PALETTE[2]\n\n# Columns: set these to your table's column names\nSOURCE = \"Researcher\"  # role: source\nTARGET = \"Researcher\"  # role: target\nWEIGHT = \"Collaboration strength\"  # role: weight\n\n# Data: replace this block with your own square matrix, for example\n# df = pd.read_csv(\"adjacency.csv\", index_col=0); rows and columns share the node order,\n# absent edges are NaN\nnp.random.seed(42)\nnames = [\n    \"Alice\", \"Bob\", \"Carol\", \"Dan\", \"Eve\", \"Frank\", \"Grace\", \"Hank\", \"Iris\", \"Jack\",  # Dept A\n    \"Kate\", \"Leo\", \"Mia\", \"Ned\", \"Olivia\", \"Paul\", \"Quinn\", \"Rose\", \"Sam\", \"Tina\",  # Dept B\n    \"Uma\", \"Victor\", \"Wendy\", \"Xavier\", \"Yara\", \"Zane\", \"Anna\", \"Blake\", \"Cleo\", \"Derek\",  # Dept C\n]  # fmt: skip\ndept = np.arange(len(names)) // 10\nsame_dept = dept[:, None] == dept[None, :]\nhas_edge = np.triu(np.random.rand(len(names), len(names)) < np.where(same_dept, 0.70, 0.12), k=1)\nstrength = np.where(\n    same_dept, np.random.uniform(0.4, 1.0, same_dept.shape), np.random.uniform(0.05, 0.35, same_dept.shape)\n)\nupper = np.where(has_edge, strength, np.nan)\nmatrix = np.where(np.isnan(upper), upper.T, upper)  # undirected: mirror across the diagonal\ndf = pd.DataFrame(matrix, index=names, columns=names)\n\n# Plot: reads only df and the constants above\norder = list(df.index)\nlong = df.rename_axis(index=\"row\", columns=\"col\").stack(future_stack=True).rename(WEIGHT).reset_index()\nlong[\"row\"] = pd.Categorical(long[\"row\"], categories=order[::-1], ordered=True)  # first node at the top\nlong[\"col\"] = pd.Categorical(long[\"col\"], categories=order, ordered=True)\n\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG),\n    panel_grid=element_blank(),\n    axis_title=element_text(color=INK, size=14),\n    axis_text=element_text(color=INK_SOFT, size=9),\n    axis_text_x=element_text(color=INK_SOFT, size=9, angle=90, hjust=1),\n    axis_ticks=element_blank(),\n    axis_line=element_blank(),\n    plot_title=element_text(color=INK, size=16),\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)\n\nplot = (\n    ggplot(long, aes(x=\"col\", y=\"row\", fill=WEIGHT))\n    + geom_tile()\n    + scale_fill_gradient(low=SEQ_LOW, high=SEQ_HIGH, na_value=PAGE_BG, name=WEIGHT.replace(\" \", \"\\n\", 1))\n    + labs(title=\"heatmap-adjacency · python · letsplot · anyplot.ai\", x=SOURCE, y=TARGET)\n    + anyplot_theme\n    + ggsize(600, 600)\n)\n\n# Save\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}