1
votes

I want to wrap text, as in if it is a long sentence, add line breaks so the font has bigger size, for example, like here enter image description here

I have the following data that can be downloaded from https://gofile.io/d/8djNCU:

enter image description here

Here is my code:

import plotly.express as px
import pandas as pd

df = pd.read_excel("/Users/stuff/Desktop/test.xlsx")

g1 = df.Group1
g2 = df.Group2
g3 = df.Group3
g4 = df.Group4
my_values = df.Values

df = pd.DataFrame(
    dict(group1=g1, group2=g2, group3=g3, group4=g4, my_values=my_values)
)

fig = px.sunburst(df, path=['group1', 'group2', 'group3', 'group4'], values='my_values')

fig.show()

The current sunburst diagram looks like this: enter image description here

Is it possible to wrap the text? I would like the long text to be as big as possible and split by break line as I would like to use the image for a presentation.

1
What do you mean by wrap the text? Do you mind to clarify?rpanai
Please check the update.Gabriele

1 Answers

4
votes

By adjusting the width in the customwrap function, I was able to make a nice sunburst. The answer was provided by the plotly's GitHub community.

import plotly.express as px
import pandas as pd
import textwrap

def customwrap(s,width=30):
    return "<br>".join(textwrap.wrap(s,width=width))

df = pd.read_excel("/Users/stuff/Desktop/test.xlsx")

g1 = df.Group1.map(customwrap)
g2 = df.Group2.map(customwrap)
g3 = df.Group3.map(customwrap)
g4 = df.Group4.map(customwrap)
my_values = df.Values

df = pd.DataFrame(
    dict(group1=g1, group2=g2, group3=g3, group4=g4, my_values=my_values)
)
fig = px.sunburst(df, path=['group1', 'group2', 'group3', 'group4'], values='my_values')

fig.show()