Equalize sunburst section widths per level

Cheers,

the sunburst plot as it is sets the section width angle of a section on a given level to the sum of its children and their children recursively. This is why Seth in this plot

go.Figure(go.Sunburst(
    labels= [ "Eve", "Cain", "Seth", "Enos", "Noam", "Abel", ],
    parents=[    "",  "Eve",  "Eve", "Seth", "Seth", "Eve", ],
    sort=False
))

sunburst_question_1

… gets more real estate than his siblings.

And he “inherits” (backwards in time) this phenomenon from Enos:

go.Figure(go.Sunburst(
    labels= [ "Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Eric", "Ellis", "Evan", "Emille" ],
    parents=[    "",  "Eve",  "Eve", "Seth", "Seth", "Eve",  "Enos", "Enos",  "Enos", "Enos", ],
    sort=False
))

sunburst_question_2

However, what I want is for each member on their hierarchical level to get equal space. For the play dataset, we can hack our way there by adjusting the values (in my use case, values don’t matter, the plot should only make a hierarchy of categories browsable):

go.Figure(go.Sunburst(
    labels= [ "Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Eric", "Ellis", "Evan", "Enzo" ],
    parents=[    "",  "Eve",  "Eve", "Seth", "Seth", "Eve",  "Enos", "Enos",  "Enos", "Enos", ],
    values= [    24,      8,      8,      4,      4,     8,       1,      1,       1,      1, ],
    branchvalues="total", sort=False
))

sunburst_question_3

But is it possible to plot a larger (non-play) dataset in this way, or to compute the necessary numbers for this “hack” procedurally? In my use case, the display depth is 2, so users only ever see one category and its children, except when on “root”.

For those interested, I solved this by doing exactly what I termed “hack” previously. Programmatically, it consists in actually giving each entry its absolute width as a value. If entries in the dataframe (I used pandas) don’t have their level explicit (in which case you can simlpy iterate through levels using groupby and in each group, set value to 1 / groupsize), you can go through the hierarchy recursively and give each group of children to a parent entry their parent’s value multiplied with the number of children it has.

I wonder, though, if setting the section sizes to “equal” might be a sensible option to the branchvalues parameter.

1 Like