Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the colors on a plotly sunburst chart using the graph_objects submodule?

If I’m using the plotly express submodule, I can create a colored sunburst chart in the following way:

import plotly.express as px


names = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ]
values = [10, 14, 12, 10, 2, 6, 6, 4, 4]
color_discrete_sequence = ['#2D5F91', '#FFAA00', '#96BEE6', '#48B7B4', '#E8655F',
                           '#819FBD', '#FFCC66', '#C0D8F0', '#91D4D2', '#F1A39F']

fig = px.sunburst(
    names=names,
    parents=parents,
    values=values,
    color_discrete_sequence = color_discrete_sequence,
)
fig.show()

I would like to do the same with plotly’s graph_objects. Is there a similar functionality?

like image 892
Tobias Avatar asked Dec 29 '25 04:12

Tobias


1 Answers

The graph_objects version is available in the official reference.

import plotly.graph_objects as go

color_discrete_sequence = ['', '#FFAA00', '#2D5F91','#819FBD','#819FBD','#91D4D2', '#96BEE6', '#C0D8F0','#E8655F','#F1A39F','#48B7B4']

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=\[10, 14, 12, 10, 2, 6, 6, 4, 4\],
    marker=dict(colors=color_discrete_sequence)
))
# Update layout for tight margin
# See https://plotly.com/python/creating-and-updating-figures/
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))

fig.show()

enter image description here

like image 109
r-beginners Avatar answered Dec 30 '25 18:12

r-beginners



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!