Search code examples
pythonplotlysunburst-diagram

How can I start a python plotly.express sunburst chart in a preselected starting position?


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


Solution

  • 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