2

tried the example in an earlier question but I cannot get it to "render" properly:

# imports
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

# data
df = pd.DataFrame({'BA':  ['BA1', 'BA2', 'BA3', 'BA4','BA2'],
                   'RS':   [12, 13,15, 20, 18],
                   'RC':    ['medium','medium', 'high','high','high'] })

# plotly express figure
fig = px.sunburst(df, path=["BA", "RC"])

fig.show()

# plotly graph_objects figure
fig2=go.Figure(go.Sunburst(
                labels=fig['data'][0]['labels'].tolist(),
                parents=fig['data'][0]['parents'].tolist(),
                            )
                )
fig2.show()

results in:

enter image description here enter image description here

What am I doing wrong? (i expected it to look like the first picture).. using conda + jupyter lab

1 Answer 1

4

If you take a look at fig['data'], you will see that there is a field called ids which tells Plotly how to connect the parents to the labels. You need to specify this as a parameter as well.

EDIT: if you want to display values the same way as px.sunburst, you also need to include the parameter branchvalues='total'

# plotly graph_objects figure
fig2=go.Figure(go.Sunburst(
    branchvalues='total',
    ids=fig['data'][0]['ids'].tolist(),
    labels=fig['data'][0]['labels'].tolist(),
    parents=fig['data'][0]['parents'].tolist(),
    values=fig['data'][0]['values'].tolist()
    )
)
fig2.show()

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

No problem, glad to have helped :)
Derek, when adding the values (values=sbfig['data'][0]['values'].tolist()) then the rendering "breaks" again, what am I still not getting? :)
That seems strange... I'll look into this when I have the chance!
I think as a general rule, if you are trying to recreate a plotly fig, you should always look into fig['data'] to make sure you are including everything important. Include the parameter branchvalues='total' and you should get the expected sunburst chart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.