Masih
Masih

Reputation: 15

Customizing hover info of dash plotly

I am using the clustergram from dash bio and was wondering whether there is a way to change the hover keys from 'x,y,z' to desired labels which is feasible in plotly? By changing hover_info, one can select to choose what to be shown in hover but it doesn't allow for changing the keys. Here is the code I test.

import pandas as pd
import dash_core_components as dcc
import dash_bio as dashbio

df = pd.read_csv("https://git.io/clustergram_brain_cancer.csv").set_index("ID_REF")

columns = list(df.columns.values)
rows = list(df.index)

clustergram = dashbio.Clustergram(
    data=df.loc[rows].values,
    row_labels=rows,
    column_labels=columns,
#     color_threshold={
#         'row': 250,
#         'col': 700
#     },
    color_list={
        'row': 'black',
        'col': 'black',
        'bg': 'black'
    },
    color_map= [
        [0.0, "#339AF0"],
        [0.5, "#FFFFFF"],
        [1.0, "#E64980"]],
    height=800,
    width=700,
    display_ratio=[0.7, 0.3]
)

#clustergram
res=dcc.Graph(figure=clustergram)
for i in res.to_plotly_json()['props']['figure']['data']:
  i.update({'hoverinfo':['x','y','z']})
clustergram

Upvotes: 1

Views: 1372

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

  • all example return a plotly figure
  • hence can use normal techniques to manipulate a figure
clustergram.for_each_trace(
    lambda t: t.update(hovertemplate="custom x: %{x}<br>y %{y}<br>z %{z}")
    if isinstance(t, go.Heatmap)
    else t
)

full code

import pandas as pd
import dash_core_components as dcc
import dash_bio as dashbio
import plotly.graph_objects as go

df = pd.read_csv("https://git.io/clustergram_brain_cancer.csv").set_index("ID_REF")

columns = list(df.columns.values)
rows = list(df.index)

clustergram = dashbio.Clustergram(
    data=df.loc[rows].values,
    row_labels=rows,
    column_labels=columns,
    color_threshold={"row": 250, "col": 700},
    height=800,
    width=700,
    color_map=[
        [0.0, "#636EFA"],
        [0.25, "#AB63FA"],
        [0.5, "#FFFFFF"],
        [0.75, "#E763FA"],
        [1.0, "#EF553B"],
    ],
)

clustergram.for_each_trace(
    lambda t: t.update(hovertemplate="custom x: %{x}<br>y %{y}<br>z %{z}")
    if isinstance(t, go.Heatmap)
    else t
)

Upvotes: 1

Related Questions