Treemap Hovertext

Hello,

I am trying to create a treemap. The dataframe df_all_trees has some more columns which I would like to display as information on hovering. How do I add this data into a variable that can be then used in hovertemplate?

fig.add_trace(go.Treemap(
    labels=df_all_trees['id'],
    parents=df_all_trees['parent'],
    values=df_all_trees['value'],
    branchvalues='total',
    marker=dict(
        colors=df_all_trees['color'],
        colorscale='RdBu',
        cmid=average_score),
    hovertemplate='<b>%{label} </b> <br> Last Close: %{value}',
    name='' ), 1, 1)

Hi @saboo.anuj17 you can add your additional data in the customdata attribute of go.Treemap and then access the customdata in the hovertemplate as described in https://plotly.com/python/hover-text-and-formatting/#adding-other-data-to-the-hover-with-customdata-and-a-hovertemplate.

Maybe you could also use px.treemap here, in which case you can pass the additional columns to the hover_data argument of px.treemap, and then they will be added automatically to the hovertemplate. See for example the example in https://plotly.com/python/plotly-express/

import plotly.express as px
df = px.data.tips()
fig = px.violin(df, y="tip", x="smoker", color="sex", box=True, points="all", hover_data=df.columns)
fig.show()

Hi @Emmanuelle thanks for your help. I was not able to get around the array stacking to get to my desired solution, however the px.treemap case worked well. Thank you very much.

In addition to this, would you be able to help me to disable hover text for hierarchies? I am trying to build a multi-level treemap and I do not want the hovertext to display for the hierarchies because the respective values would not be present. Eg. For stock market, I would have data for stock but not for it’s sector(eg. IT).

Hi @saboo.anuj17 if you want to remove information from the default hover, you’ll need to modify the hovertemplate of the figure. You can do print(fig) to see the hovertemplate which is used in your current figure and modify it with fig.update_traces(hovertemplate=...).

1 Like

Hi @Emmanuelle I realize this is an old conversation, but it has helped me along. I have one small follow-up question: is there a way to have the print(fig) output be more verbose? In my session, it is abridging with a β€œβ€¦β€ string the section I am most interested in. :-/

Hi @kehliah,

According to: Hover Text and Formatting | Python | Plotly

the following print command should work:
print(fig.data[0].hovertemplate)