1

I'm trying to plot an interactive dendrogram with an unequal heatmap on jupyter notebook using Plotly package that would look like this one. What I'm trying to plot

My example data called dataHeat_arr is numpy.ndarray, which has 75 rows (samples called S0 till S74) and 100 columns (Metabolites called M0 till M99) and available in the link.

In the link there are 3 csv files.

  1. dataHeat_arr.csv - the numpy.ndarray 75*100
  2. name_molec.csv - list of metbolite named M0, M1,... till M99
  3. Samplenum.csv - list of samples named S0, S1,... till S74

I based my code on the example from her and made some changes since my the heatmat is unequale.

Also tried questoins Plotly clustered heatmap (with dendrogram)/Python

I do not know what I'm doning worng by the finale figure is missig the heat map. only when ploting with equale heatmap I manage to plot the heatmap with the dendrogram.

her is my code.

Import packges:

import plotly.figure_factory as ff
import numpy as np
np.random.seed(1)
import pandas as pd
import numpy as np
import string 
from itables import init_notebook_mode
from itables import show
import cimcb_lite as cb
import plotly.graph_objects as go
init_notebook_mode(all_interactive=True)

Initialize figure by creating upper dendrogram:

# name the samples S0 till S74

# Samplenum & name_molec are csv files in the link of Github and should be treated as lists
labels = Samplenum
dataHeat_arr_t= np.transpose(dataHeat_arr)

# Initialize figure by creating upper dendrogram
fig = ff.create_dendrogram(dataHeat_arr, orientation='bottom', labels=labels)

# fig = ff.create_dendrogram(dataHeat_arr_t, orientation='bottom', labels=name_molec[:100] ) ,labels=name_molec[:100]

for i in range(len(fig['data'])):
    fig['data'][i]['yaxis'] = 'y2'

Then Create Side Dendrogram:

# Create Side Dendrogram

dendro_side = ff.create_dendrogram(dataHeat_arr_t, orientation='right' ,labels=name_molec[:100])
# dendro_side = ff.create_dendrogram(dataHeat_arr, orientation='right', labels=labels)
for i in range(len(dendro_side['data'])):
    dendro_side['data'][i]['xaxis'] = 'x2'
     
# Add Side Dendrogram Data to Figure
for data in dendro_side['data']:
    fig.add_trace(data)

Create Heatmap:

heatmap = [
    go.Heatmap(
        x = name_molec[:100],
        y =labels ,
        z = dataHeat_arr,
        colorscale = 'Cividis'
    )
]

Add Heatmap Data to Figure:

for data in heatmap:
    fig.add_trace(data)

Now layout:

# Edit Layout
fig.update_layout({'width':1500, 'height':750,
                         'showlegend':False, 'hovermode': 'closest',
                         })
# Edit xaxis
fig.update_layout(xaxis={'domain': [.15, 1],
                                  'mirror': False,
                                  'showgrid': False,
                                  'showline': False,
                                  'zeroline': False,
                                  'ticks':""})

# Edit xaxis2
fig.update_layout(xaxis2={'domain': [0, .15],
                                   'mirror': False,
                                   'showgrid': False,
                                   'showline': False,
                                   'zeroline': False,
                                   'showticklabels': False,
                                   'ticks':""})

# Edit yaxis
fig.update_layout(yaxis={'domain': [0, .85],
                                  'mirror': False,
                                  'showgrid': False,
                                  'showline': False,
                                  'zeroline': False,
                                  'showticklabels': False,
                                  'ticks': ""
                        })

# Edit yaxis2
fig.update_layout(yaxis2={'domain':[0.852, 0.975],
                                   'mirror': False,
                                   'showgrid': False,
                                   'showline': False,
                                   'zeroline': False,
                                   'showticklabels': False,
                                   'ticks':""})

fig.show()

for some reason the plot looks like that:

dendrogram without heatmap

Any idea/ hint will be usfull and appriciated!

1 Answer 1

1

The correct code to plot heatmap with dendrogram on both y-axis an x-aixs:

Initialize figure by creating upper dendrogram:

Samplenum = ["S" + str(x) for x in idx]
labels = Samplenum
dataHeat_arr_t= np.transpose(dataHeat_arr)
# Initialize figure by creating upper dendrogram

# The line below was changed comparing to my question 
fig = ff.create_dendrogram(dataHeat_arr_t, orientation='bottom', labels=name_molec[:99] )


for i in range(len(fig['data'])):
    fig['data'][i]['yaxis'] = 'y2'

Create Side Dendrogram


# The line below was changed as well comparing to my question 
dendro_side = ff.create_dendrogram(dataHeat_arr, orientation='right', labels=["S" + str(x) for x in idx])
for i in range(len(dendro_side['data'])):
    dendro_side['data'][i]['xaxis'] = 'x2'
    
    
# Add Side Dendrogram Data to Figure
for data in dendro_side['data']:
    fig.add_trace(data)

Create Heatmap:

heatmap = [
    go.Heatmap(
        x = name_molec[:99],
        y =labels ,
        z = dataHeat_arr,
        colorscale = 'Cividis'
    )
]

These 4 lines are new ( were not in my question):

heatmap[0]['x'] = fig['layout']['xaxis']['tickvals']
heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals']


# to tickes text on y-axis as well
fig['layout']['yaxis']['ticktext'] = np.asarray(labels)
fig['layout']['yaxis']['tickvals'] = np.asarray(dendro_side['layout']['yaxis']['tickvals'])

Most of the layout code stays the same but :showticklabels': True

# Edit yaxis
fig.update_layout(yaxis={'domain': [0, .7],
                                  'mirror': False,
                                  'showgrid': False,
                                  'showline': False,
                                  'zeroline': False,
                                  'showticklabels': True,
                                  'ticks': ""
                        })

The output looks more like the picture at beginning of my question.

my finale plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.