Search code examples
pythoncolorsplotlysunburst-diagram

How to control the color input of a Sunburst with plotly.graph_objects?


I would like to have control over the colors of each label in the Sunburst diagram below - when using plotly.graph_objects instead of plotly.express.

See example from documentation below:

import plotly.graph_objects as go

fig =go.Figure(go.Sunburst(
    labels=[ "Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parents=["",    "Eve",  "Eve",  "Seth", "Seth", "Eve",  "Eve",  "Awan",  "Eve" ],
    values=[  65,    14,     12,     10,     2,      6,      6,      4,       4],
    branchvalues="total",
))
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))

fig.show()

enter image description here


Solution

  • That would allow accessing and control the markers:

    import plotly.express as px
    
    greys = px.colors.sequential.Greys
    reds = px.colors.sequential.Reds
    blues = px.colors.sequential.Blues
    greens = px.colors.sequential.Greens
    magents = px.colors.sequential.Magenta
    
    fig.data[0].marker.colors = ['', greys[5], reds[5], reds[6], reds[7], 
                                 blues[5], greens[5], greens[6], magents[5]]
    
    fig.show()
    

    enter image description here