{ "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": [ "
Tooltips appear for all nodes, though markers are visible only for root nodes of subtrees.
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.