{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Components of dendrogram\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The :func:`~pdendro.make_components` function allows obtaining dendrogram components without\nattaching them to a figure.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import scipy\n\nfrom pdendro import make_components\n\nX = [[0.0], [1.0], [3.0], [6.0]]\nZ = scipy.cluster.hierarchy.linkage(X)\n\ncomponents = make_components(Z, subtrees={2, 4})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function returns a :class:`tuple` object containing four items: the first item represents\nnodes, the second links, the third an axis, and the fourth the other axis.\n\nThe following figure shows the items separately.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly.graph_objects as go\nimport plotly.io as pio\n\nfig = go.Figure()\nfig.set_subplots(rows=2, cols=2, subplot_titles=[f\"out[{i}]\" for i in range(len(components))])\nfig.add_trace(components[0], row=1, col=1)\nfig.add_traces(list(components[1].values()), rows=1, cols=2)\nfig.update_xaxes(components[2], row=2, col=1)\nfig.update_yaxes(components[3], row=2, col=2)\n\n# for better clarity\nfor row, col in [(1, 1), (1, 2), (2, 2)]:\n fig.update_xaxes(matches=\"x3\", visible=False, row=row, col=col)\nfor row, col in [(1, 1), (1, 2), (2, 1)]:\n fig.update_yaxes(matches=\"y4\", visible=False, row=row, col=col)\nfor col in [1, 2]:\n fig.add_scatter(x=[], y=[], row=2, col=col)\n\npio.show(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Note

Tooltips appear for all nodes, though markers are visible only for root nodes of subtrees.

\n\n

Note

The third and fourth items do not always represent the x- and y-axes. They depend on\n the `direction` parameter of the :func:`~pdendro.make_components` function.

\n\nAdditionally, the second item contains separate subtrees.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = go.Figure()\nfig.set_subplots(\n rows=1,\n cols=len(components[1]),\n subplot_titles=[f\"components[1][{node_id}]\" for node_id in components[1]],\n)\nfor col, subtree in enumerate(components[1].values(), start=1):\n fig.add_trace(subtree, row=1, col=col)\n\n# for better clarity\nfig.update_xaxes(components[2], visible=False, row=1, col=1)\nfig.update_yaxes(components[3], visible=False, row=1, col=1)\nfig.update_xaxes(matches=\"x\", visible=False, row=1, col=2)\nfig.update_yaxes(matches=\"y\", visible=False, row=1, col=2)\n\npio.show(fig)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.4" } }, "nbformat": 4, "nbformat_minor": 0 }