""" Components of dendrogram ======================== """ # %% # The :func:`~pdendro.make_components` function allows obtaining dendrogram components without # attaching them to a figure. import scipy from pdendro import make_components X = [[0.0], [1.0], [3.0], [6.0]] Z = scipy.cluster.hierarchy.linkage(X) components = make_components(Z, subtrees={2, 4}) # %% # The function returns a :class:`tuple` object containing four items: the first item represents # nodes, the second links, the third an axis, and the fourth the other axis. # # The following figure shows the items separately. import plotly.graph_objects as go import plotly.io as pio fig = go.Figure() fig.set_subplots(rows=2, cols=2, subplot_titles=[f"out[{i}]" for i in range(len(components))]) fig.add_trace(components[0], row=1, col=1) fig.add_traces(list(components[1].values()), rows=1, cols=2) fig.update_xaxes(components[2], row=2, col=1) fig.update_yaxes(components[3], row=2, col=2) # for better clarity for row, col in [(1, 1), (1, 2), (2, 2)]: fig.update_xaxes(matches="x3", visible=False, row=row, col=col) for row, col in [(1, 1), (1, 2), (2, 1)]: fig.update_yaxes(matches="y4", visible=False, row=row, col=col) for col in [1, 2]: fig.add_scatter(x=[], y=[], row=2, col=col) pio.show(fig) # %% # .. note:: # # Tooltips appear for all nodes, though markers are visible only for root nodes of subtrees. # # .. note:: # # The third and fourth items do not always represent the x- and y-axes. They depend on # the `direction` parameter of the :func:`~pdendro.make_components` function. # # Additionally, the second item contains separate subtrees. fig = go.Figure() fig.set_subplots( rows=1, cols=len(components[1]), subplot_titles=[f"components[1][{node_id}]" for node_id in components[1]], ) for col, subtree in enumerate(components[1].values(), start=1): fig.add_trace(subtree, row=1, col=col) # for better clarity fig.update_xaxes(components[2], visible=False, row=1, col=1) fig.update_yaxes(components[3], visible=False, row=1, col=1) fig.update_xaxes(matches="x", visible=False, row=1, col=2) fig.update_yaxes(matches="y", visible=False, row=1, col=2) pio.show(fig)