{"spec_id":"dendrogram-basic","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ndendrogram-basic: Basic Dendrogram\nLibrary: plotly 6.8.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-06-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.figure_factory as ff\nfrom scipy.cluster.hierarchy import linkage\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\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Colorscale: maps to scipy color keys (b,c,g,k,m,r,w,y) alphabetically\n# C0→b(above threshold), C1→g(cluster 1), C2→r(cluster 2), C3→c(cluster 3)\n# Imprint palette — brand green on C1 slot, lavender on C2, blue on C3\ncolorscale = [\n    INK_SOFT,  # b → above-threshold merges (theme-adaptive)\n    \"#4467A3\",  # c → Imprint blue (cluster 3)\n    \"#009E73\",  # g → Imprint green, brand — first cluster colour\n    INK_SOFT,  # k → muted ink (softens above-threshold branch rendering)\n    \"#954477\",  # m → Imprint rose\n    \"#C475FD\",  # r → Imprint lavender (cluster 2)\n    PAGE_BG,  # w → page background (theme-adaptive)\n    \"#BD8233\",  # y → Imprint ochre\n]\n\n# Data - Iris flower measurements for 15 samples across 3 species\nnp.random.seed(42)\nlabels = [\n    \"Setosa-1\",\n    \"Setosa-2\",\n    \"Setosa-3\",\n    \"Setosa-4\",\n    \"Setosa-5\",\n    \"Versicolor-1\",\n    \"Versicolor-2\",\n    \"Versicolor-3\",\n    \"Versicolor-4\",\n    \"Versicolor-5\",\n    \"Virginica-1\",\n    \"Virginica-2\",\n    \"Virginica-3\",\n    \"Virginica-4\",\n    \"Virginica-5\",\n]\n\n# Sepal length, sepal width, petal length, petal width\n# Anisotropic per-feature spread creates richer within-cluster distance variation\nsetosa = np.random.randn(5, 4) * np.array([0.25, 0.40, 0.15, 0.12]) + np.array([5.0, 3.4, 1.5, 0.2])\nversicolor = np.random.randn(5, 4) * np.array([0.55, 0.35, 0.50, 0.28]) + np.array([5.9, 2.8, 4.3, 1.3])\nvirginica = np.random.randn(5, 4) * np.array([0.50, 0.30, 0.55, 0.32]) + np.array([6.6, 3.0, 5.5, 2.0])\ndata = np.vstack([setosa, versicolor, virginica])\n\n# Plot - Ward linkage dendrogram with Imprint cluster colouring\nfig = ff.create_dendrogram(\n    data, labels=labels, colorscale=colorscale, linkagefun=lambda x: linkage(x, method=\"ward\"), color_threshold=3.5\n)\n\n# Update branch widths and add merge-distance hover\nfor trace in fig.data:\n    trace.line.width = 3\n    max_y = max((y for y in trace.y if y is not None and y > 0), default=0)\n    trace.hovertemplate = f\"Merge distance: {max_y:.2f}<extra></extra>\"\n\n# Dashed cut-threshold line marks the cluster boundary\nfig.add_shape(\n    type=\"line\",\n    x0=0,\n    x1=1,\n    xref=\"paper\",\n    y0=3.5,\n    y1=3.5,\n    yref=\"y\",\n    line={\"color\": INK_SOFT, \"width\": 2, \"dash\": \"dash\"},\n)\nfig.add_annotation(\n    x=0.98,\n    xref=\"paper\",\n    y=3.5,\n    text=\"cut threshold = 3.5\",\n    showarrow=False,\n    font={\"color\": INK_SOFT, \"size\": 10},\n    xanchor=\"right\",\n    yanchor=\"bottom\",\n)\n\ntitle = \"dendrogram-basic · python · plotly · anyplot.ai\"\n\nfig.update_layout(\n    autosize=False,\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    title={\"text\": title, \"font\": {\"size\": 16, \"color\": INK}, \"x\": 0.5, \"xanchor\": \"center\"},\n    xaxis={\n        \"title\": {\"text\": \"Iris Flower Samples\", \"font\": {\"color\": INK, \"size\": 12}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"tickangle\": -35,\n        \"showline\": False,\n        \"zeroline\": False,\n        \"mirror\": False,\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Distance (Ward linkage)\", \"font\": {\"color\": INK, \"size\": 12}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"showline\": False,\n        \"zeroline\": False,\n        \"mirror\": False,\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n    },\n    xaxis2={\n        \"visible\": False,\n        \"showticklabels\": False,\n        \"showline\": False,\n        \"showgrid\": False,\n        \"zeroline\": False,\n        \"ticks\": \"\",\n    },\n    yaxis2={\n        \"visible\": False,\n        \"showticklabels\": False,\n        \"showline\": False,\n        \"showgrid\": False,\n        \"zeroline\": False,\n        \"ticks\": \"\",\n    },\n    margin={\"l\": 80, \"r\": 40, \"t\": 80, \"b\": 100},\n    hoverlabel={\"bgcolor\": ELEVATED_BG, \"font_size\": 12, \"bordercolor\": INK_SOFT},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}