kg75
kg75

Reputation: 31

Plotly: Follow-up How to create sunburst subplot using graph_objects?

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

Upvotes: 2

Views: 1007

Answers (1)

Derek O
Derek O

Reputation: 19600

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

Upvotes: 4

Related Questions