{"spec_id":"heatmap-adjacency","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nheatmap-adjacency: Network Adjacency Matrix Heatmap\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 81/100 | Created: 2026-05-08\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this file from shadowing the installed plotly package\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p) != _here]\ndel _here\n\nimport numpy as np\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\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Data — 5 research domains, 6 terms each = 30 nodes\nnp.random.seed(42)\n\ndomains = {\n    \"Machine Learning\": [\"neural net\", \"gradient\", \"backprop\", \"optimizer\", \"dropout\", \"attention\"],\n    \"Neuroscience\": [\"synapse\", \"cortex\", \"dendrite\", \"neuron\", \"hippocampus\", \"axon\"],\n    \"Genomics\": [\"DNA\", \"RNA\", \"gene\", \"protein\", \"chromosome\", \"mutation\"],\n    \"Chemistry\": [\"molecule\", \"catalyst\", \"reaction\", \"bond\", \"polymer\", \"enzyme\"],\n    \"Physics\": [\"quantum\", \"photon\", \"entropy\", \"wave\", \"spin\", \"field\"],\n}\n\nnodes = []\nnode_domain_idx = []\nfor di, (_, terms) in enumerate(domains.items()):\n    nodes.extend(terms)\n    node_domain_idx.extend([di] * len(terms))\n\nn = len(nodes)\n\n# Symmetric co-occurrence matrix with block-diagonal cluster structure\nmatrix = np.zeros((n, n))\nfor i in range(n):\n    for j in range(i + 1, n):\n        if node_domain_idx[i] == node_domain_idx[j]:\n            val = np.random.uniform(0.55, 1.0)\n        else:\n            val = np.random.uniform(0.0, 0.12)\n        matrix[i, j] = val\n        matrix[j, i] = val\n\n# Cluster boundary positions (between consecutive domain groups)\ndomain_size = 6\nboundaries = [domain_size * k for k in range(1, len(domains))]\n\n# Plot\nfig = go.Figure(\n    go.Heatmap(\n        z=matrix,\n        x=nodes,\n        y=nodes,\n        colorscale=\"viridis\",\n        zmin=0,\n        zmax=1,\n        colorbar={\n            \"title\": {\"text\": \"Co-occurrence<br>strength\", \"font\": {\"size\": 20, \"color\": INK}, \"side\": \"right\"},\n            \"tickfont\": {\"size\": 16, \"color\": INK_SOFT},\n            \"thickness\": 28,\n            \"len\": 0.78,\n        },\n        hovertemplate=\"%{y} ↔ %{x}<br>Strength: %{z:.2f}<extra></extra>\",\n    )\n)\n\n# Cluster boundary lines separating research domains\nfor b in boundaries:\n    fig.add_shape(\n        type=\"line\", x0=b - 0.5, x1=b - 0.5, y0=-0.5, y1=n - 0.5, line={\"color\": INK, \"width\": 2.5}, xref=\"x\", yref=\"y\"\n    )\n    fig.add_shape(\n        type=\"line\", x0=-0.5, x1=n - 0.5, y0=b - 0.5, y1=b - 0.5, line={\"color\": INK, \"width\": 2.5}, xref=\"x\", yref=\"y\"\n    )\n\nfig.update_layout(\n    title={\n        \"text\": \"Research Domain Co-occurrence · heatmap-adjacency · plotly · anyplot.ai\",\n        \"font\": {\"size\": 24, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    xaxis={\n        \"tickfont\": {\"size\": 13, \"color\": INK_SOFT}, \"tickangle\": -45, \"showgrid\": False, \"linecolor\": INK_SOFT, \"zeroline\": False\n    },\n    yaxis={\n        \"tickfont\": {\"size\": 13, \"color\": INK_SOFT}, \"showgrid\": False, \"linecolor\": INK_SOFT, \"zeroline\": False, \"autorange\": \"reversed\"\n    },\n    margin={\"l\": 130, \"r\": 150, \"t\": 80, \"b\": 170},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1200, height=1200, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}