8

Currently, plotly express treemap shows only label inside treemap. How to include the value alongside the label?

imdevskp
  • 2,103
  • 2
  • 9
  • 23

1 Answers1

21

That's why I don't like express, there are too many limitations and to make these kinds of changes you have to access the trace either way. From my point of view it is better and more code-transparent to use plain plotly instead.

That being said, you can access the textinfo attribute of the trace to do this. From the reference:

Determines which trace information appear on the graph.

Any combination of "label", "text", "value", "current path", "percent root", "percent entry", "percent parent" joined with a "+" OR "none".

Taking an example from the site:

df = px.data.tips()
fig = px.treemap(df, path=['day', 'time', 'sex'], values='total_bill')

# this is what I don't like, accessing traces like this
fig.data[0].textinfo = 'label+text+value+current path'

fig.layout.hovermode = False
fig.show()

example

Also take a look at the texttemplate attribute for formatting options.

Community
  • 1
  • 1
vlizana
  • 2,962
  • 1
  • 16
  • 26
  • Even though you don't like it, still a nice answer, thanks. Is it also possible to show the aggregated values or percentages in the outer boxes? In your example the outer box should have: SAT 1,778.4 and the inner box could have Dinner 1,778.4 – jugi Apr 09 '20 at 14:38
  • 2
    @jugi it is possible with a workaround. You could preformat your data to add it to the actual label text and make sure you're using an id column instead of relying on label only. Then it would read something like Sat (3,034.34) > Dinner (2,234.23) > Male (1,227.35), etc – Brendan Dec 12 '20 at 04:35
  • Can you comment on why this affects only the label for the leaf node, but none of the parent nodes? – Uncle Long Hair Nov 11 '22 at 15:40
  • @Brendan thank you for the idea. I have an unanswered question about it [here](https://stackoverflow.com/questions/73363141/display-aggregate-value-next-to-label-in-frame-in-treemap) and have been looking for a solution. – ou_ryperd Mar 28 '23 at 07:39
  • @Brendan Funny, the values are in the hover data, but there doesn't seem to be a property to display it :-/ – ou_ryperd Mar 30 '23 at 12:34
  • px is great. keeping the api for treemap is minimal is understandable because the path makes things complex, but percentages belong in a high-level api – Kermit Jul 01 '23 at 12:11