Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using plotly.graph_objects, is there way to trim or wrap the label text inside of each box when the box is too small?

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

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.

Proposed solution 1: Force the font size to be bigger, hide if it won't fit.

By adding this line:

    fig.update_layout(uniformtext=dict(minsize=11, mode='hide'))

It hides labels that don't fit:

enter image description here

And that's not very good. I want to be able to see the text.

Proposed solution 2: Force the font size to be bigger, force show if it won't fit.

    fig.update_layout(uniformtext=dict(minsize=11, mode='show'))

enter image description here

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?

Proposed solution 3: Trim text ahead of time.

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.

like image 892
zumalifeguard Avatar asked Oct 29 '25 21:10

zumalifeguard


1 Answers

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()

enter image description here

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()     

enter image description here

like image 199
Phoenix Avatar answered Nov 02 '25 05:11

Phoenix



Donate For Us

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