If you look here: https://plotly.com/python/treemaps/#nested-layers-in-treemap, looking at the image:

Note the tiny text in the second small block. You can hardly see it because the text is "Blackcurrant-like", and it's too big to fit.
None of the current options proposed in the documentation are sufficient.
By adding this line:
fig.update_layout(uniformtext=dict(minsize=11, mode='hide'))
It hides labels that don't fit:

And that's not very good. I want to be able to see the text.
fig.update_layout(uniformtext=dict(minsize=11, mode='show'))

That solves the problem in the text hiding, but now it draws outside of the box, which is not good.
What I would like is for the text to wrap. If that's not possible, then I would like it to trim (possibly with ellipses). I can't figure out a way to do either with this library. So my first question is, is it possible?
If I knew ahead of time that the text won't fit, I could trim it. But how do I know that, and how can I tell by how much I can trim it?
I suppose, since the the size of the box is indirectly related to the value we pass it, we can make a guess as to which boxes are going to be too small for the text we pass in. But how? I don't even know the shame of the box. Below it's a tall rectangle. There's no way for me to guess that.
If there was an event I can hook on where the UI calls me back before rending the text, giving me the dimensions of the square, allowing me to change the text, I might have a chance.
Any suggestions?
Seems silly to have all that space in the box that I can't populate. I'm assuming there's some technical reason why the text can't wrap since that would have been the most obvious solution.
You can wrap your text manually by using <br> among the text, take this example here:
import plotly.express as px
df = px.data.tips()
fig = px.treemap(df, path=[px.Constant("all"), 'day', 'time', 'sex'], values='total_bill')
fig.update_traces(root_color="lightgrey")
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

I will add a long label with manual wrapping.
fig.data[0]['labels'][0:6] = "This is too long label <br> but it is wrapped <br> into three lines"
fig.data[0]['textfont']['size'] = 20
fig.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With