Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17389

Title: "Plotly: Connecting tips between dendrograms in side-by-side subplots"

$
0
0

I'm trying to create side-by-side dendrogram subplots using Plotly in Python. I've successfully created the subplots and added dendrograms to each subplot, but I'm struggling to connect the corresponding tips between the two dendrograms with lines.

Here's a minimal example of my code:

import plotly.figure_factory as fffrom plotly.subplots import make_subplotsimport numpy as npX1 = np.random.rand(10, 12)X2 = np.random.rand(10, 12)names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark', 'Alice', 'Charlie', 'Rob', 'Lisa', 'Lily']# Create dendrogramsfig1 = ff.create_dendrogram(X1, orientation='right', labels=names)fig2 = ff.create_dendrogram(X2, orientation='left', labels=names)# Create subplots with 1 row and 2 columns#fig = make_subplots(rows=1, cols=2, subplot_titles=("Dendrogram 1", "Dendrogram 2"))# Add traces from each dendrogram to respective subplots#fig.add_traces(fig1['data'], rows=1, cols=1)#fig.add_traces(fig2['data'], rows=1, cols=2)# Calculate coordinates for the lines#line_x = [0, 0.5, 1]#line_y = [0, 0.5, 1]# Create subplots with 1 row and 2 columnsfig = make_subplots(rows=1, cols=2)# Add the first dendrogram to the first subplotfor trace in fig1['data']:    fig.add_trace(trace, row=1, col=1)# Add the second dendrogram to the second subplotfor trace in fig2['data']:    fig.add_trace(trace, row=1, col=2)# Add lines between corresponding tipsfor i in range(len(names)):    fig.add_shape(type="line",                  x0=0.5, y0=fig1['layout']['yaxis']['tickvals'][i],                  x1=1.5, y1=fig2['layout']['yaxis']['tickvals'][i],                  line=dict(color="black", width=1))# Update layoutfig.update_layout(height=600, width=800, showlegend=False, title_text="Dendrograms Side by Side")# Show plotfig.show()

In this code, I'm trying to connect the tips between the two dendrograms with lines using add_shape, but the lines don't seem to be correctly positioned. How can I properly connect the corresponding tips between the dendrograms?. I also face problm while displaying tip label, when i subplot.

Thank you for your helpenter image description here

I also did like tried to draw link in extra subplot

import plotly.figure_factory as fffrom plotly.subplots import make_subplotsimport numpy as np# Generating random data for X1, X2, and X3X1 = np.random.rand(10, 12)X2 = np.random.rand(10, 12)X3 = np.random.rand(10, 12)names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark', 'Alice', 'Charlie', 'Rob', 'Lisa', 'Lily']# Create dendrograms for X1, X2, and X3fig1 = ff.create_dendrogram(X1, orientation='right')fig2 = ff.create_dendrogram(X2, orientation='right')fig3 = ff.create_dendrogram(X3, orientation='left')# Create a new list of hover labels for fig1 and fig3 (right-oriented dendrograms)hover_labels_fig1 = []hover_labels_fig3 = []for i in range(len(X1)):    for name in names:        hover_labels_fig1.append(name)        hover_labels_fig3.append(name)# Add hover labels to each trace in fig1 and fig3for trace in fig1['data']:    trace.hovertext = hover_labels_fig1for trace in fig3['data']:    trace.hovertext = hover_labels_fig3# Create subplots with increased widthfig = make_subplots(rows=1, cols=3, column_widths=[0.5, 0.1, 0.5])# Add the first dendrogram to the first subplotfor data in fig1['data']:    fig.add_trace(data, row=1, col=1)# Add the third dendrogram to the third subplotfor data in fig3['data']:    fig.add_trace(data, row=1, col=3)# Add the link between the first and third plots in the second subplotfor i in range(len(names)):    fig.add_shape(type="line",                  x0=0.5, y0=fig1['layout']['yaxis']['tickvals'][i],                  x1=1.5, y1=fig3['layout']['yaxis']['tickvals'][i],                  line=dict(color="black", width=1),                  row=1, col=2)# Update layoutfig.update_layout(height=600, width=1200, showlegend=False, title_text="Dendrograms Side by Side")# Show plotfig.show()

enter image description here

I am still trying to solve, this, kindly help


Viewing all articles
Browse latest Browse all 17389

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>