How to show color map of a value sum rather than average in plotly sunburst

Viewed 278

I have a graph that looks like this:

enter image description here

As per plotly's documentation:

If a color argument is passed, the color of a node is computed as the average of the color values of its children, weighted by their values.

I would like tha color map max value to be the money_generated_sum, which is not a column in my df but something that plotly generates itself. The column I use is money_generated.

The df follows this structure:

category      new      money_generated
dog           False    500
cat           True     800
cat           False    70
dog           False    5000
dog           True     300
import plotly.express as px
fig = px.sunburst(df, path=['category','new'], 
                      values='money_generated',
                      color = 'money_generated')
fig.show()

Is it possible to change how the color map values in plotly sunburst chart?

1 Answers

It is possible bit a bit hacky. There is probably a better method in which you change the values directly in the fig object but this method works for me:

d=fig.to_dict()
for trace in d['data']:
    trace['marker']['colors']=trace['values']

plotly.offline.plot(d, validate=False)

The for loop over the values in 'data' is only neccesary if you have multiple sunbursts in the same figure.

Related