Search code examples
pythonplotlyvisualizationtreemapsunburst-diagram

Plotly's treemap and sunburst show wrong sizes


I am trying to use plotly's sunburst and treemap to visualize data. However, I discovered that the child nodes show wrong size compared to their parents, that is, the sum of children's size does not cover that of the parents:

import plotly.graph_objects as go

fig =go.Figure(go.Treemap(
    labels= ["i_1", "i_2", "c_a", "i_3", "c_b"],
    parents= ["c_a", "c_a", "", "c_b", ""], 
    values= [3, 2, 5, 2, 2],
))

fig.show()

The output:

Treemap output

As you can see, in the raw data, i_1 and i_2 together have a sum of 5, equal their parent c_a's 5. But in the graph, the "c_a" block is not filled up by i_1 and i_2 together. Same goes for i_3 and c_b. Even though the mouseover popups show the correct values.

Same goes for the sunburst visualization:

import plotly.graph_objects as go

fig =go.Figure(go.Sunburst(
    labels= ["i_1", "i_2", "c_a", "i_3", "c_b"],
    parents= ["c_a", "c_a", "", "c_b", ""], 
    values= [3, 2, 5, 2, 2],
))

fig.show()

sunburst graph

Again, i_1 and i_2 together do not cover the whole scope of c_a.

Are the settings to force plotly to truthfully represent the proportions of the father-child sizes. To be honest, the current representation is not usable for my purpose.


Solution

  • Yes, set branchvalues="total". The default is "remainder" which adds the value to the sum of the children, as you see in your figures. You could also leave that setting alone and set the value for all non-leaf nodes to 0.