0

My goal is to show users a preselection in my python sunburst chart.

As an example if I use this code:

import plotly.express as px
from plotly.offline import plot
data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig = px.sunburst(
    data,
    names='character',
    parents='parent',
    values='value'
)
plot(fig)

When I run this code, the sunburst will open up with Eve as root. I wish to start with "Seth" already selected instead. How can I do this?

enter image description here

Alex
  • 75
  • 1
  • 5

1 Answers1

1

I think you can achieve that by level attribute in go.Sunburst instead of px.sunburst, you can set which level will be preselected. Now, if you click in the center of the preselected graph, it will take you to the original graph.

import plotly.graph_objects as go


data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig = go.Figure(go.Sunburst(
    labels=data['character'],
    parents=data['parent'],
    values=data['value'],
    level = 'Seth'
))
fig.show()

enter image description here

Hamzah
  • 8,175
  • 3
  • 19
  • 43
  • Hi Hamzah, thank you for taking time to answer this question. I used px because of the "path" feature (in my original problem I am using a dataframe), which is why this solution is good but not perfect. However I did not specify this in the question above AND I found a solution to this problem - https://stackoverflow.com/questions/61241172/plotly-how-to-create-sunburst-subplot-using-graph-objects so by adding this info it completes the answer, thanks! – Alex Oct 05 '22 at 16:37
  • I see, but how you can do it by px only without go objects? – Hamzah Oct 05 '22 at 16:41
  • I guess it is a missing feature, would be nice if someone added "level" to px. – Alex Oct 06 '22 at 08:05