Nested Pie charts

I took the following picture from this post. Linking traces in nested pie chart (for legend toggle functionality)

I have data that would recreate this nested pie chart however I cant seem to find the way to connect data from the outer donut with the inner donut so it will show the outer donut as part of the inner donut percentage.

Any help or guidance would be appreciated.

Here’s a simplified code that recreates the following nested pie chart (also taken from the post

)

import plotly.graph_objs as go

data = [# Portfolio (inner donut)
go.Pie(values=[20,40],
labels=[‘Reds’,‘Blues’],
domain={‘x’:[0.2,0.8], ‘y’:[0.1,0.9]},
hole=0.5,
direction=‘clockwise’,
sort=False,
marker={‘colors’:[’#CB4335’,’#2E86C1’]}),
# Individual components (outer donut)
go.Pie(values=[5,15,30,10],
labels=[‘Medium Red’,‘Light Red’,‘Medium Blue’,‘Light Blue’],
domain={‘x’:[0.1,0.9], ‘y’:[0,1]},
hole=0.75,
direction=‘clockwise’,
sort=False,
marker={‘colors’:[’#EC7063’,’#F1948A’,’#5DADE2’,’#85C1E9’]},
showlegend=False)]

fig = go.Figure(data=data, layout={‘title’:‘Nested Pie Chart’})

plotly.offline.iplot(fig)

Hi @jhurtado,

I’d recommend using the new sunburst trace type for this rather than a collection of pie traces. See https://plot.ly/python/sunburst-charts/ for info on the sunburst trace type.

Hope that helps!

-Jon

Hi John,

Thank you so much for your help. In my opinion it did work but I’m missing one last thing.

I’m trying to find the way to show as proportions which it kind of does but I was just wondering If I can control
how these proportions are shown in the plot. (e.g No whitespaces in between each other and that if I have percentages it’s shown ‘areawise’ reflected in the chart) (I hope that makes sense).

Thanks again,

Try branchvalues='total':

trace = 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",
    outsidetextfont = {"size": 20, "color": "#377eb8"},
    marker = {"line": {"width": 2}},
)

layout = go.Layout(
    margin = go.layout.Margin(t=0, l=0, r=0, b=0)
)

figure = {
    'data': [trace],
    'layout': layout
}

image

Hi @chriddyp,

First of all, thanks for your help! I’m trying to add that argument but it’s not working for me. I only got a white screen.

trace = go.Sunburst(
 
  values = [36964.92, 36964.92, 36964.92, '35H', '37H', '39H', '35H', '37H', '39H', '35H', '37H', '39H'],
  
  labels = ['35H', '37H', '39H', 'ballooning_%35H', 'ballooning_%37H', 'ballooning_%39H', 'normal_propagation_%35H', 'normal_propagation_%37H', 'normal_propagation_%39H', 'unknown_%35H', 'unknown_%37H', 'unknown_%39H'],
    
  parents = [17075.199999999997, 11883.349999999999, 8006.366666666666, 903.8166666666666, 756.5333333333333, 682.0166666666667, 4454.0, 3163.6499999999996, 2311.3166666666666, 11717.383333333335, 7963.166666666666, 5013.033333333333], 
  
branchvalues='total',
  outsidetextfont={"size": 20, "color": "#377eb8"},
  leaf={"opacity": 0.4},
  marker={"line": {"width": 2}},
    
    
)

layout = go.Layout(
    margin = go.layout.Margin(t=0, l=0, r=0, b=0)
                
    
)

fig = go.Figure([trace], layout)

plotly.offline.iplot(fig)

I made sure that every branch value not was higher than the total which may create the error. May it be the way set up branches?

any help would be appreciate it.

Hi @jhurtado,

I think there may be a problem with your specification of the parents array. The elements of parents need to match elements of values, but that doesn’t seem to be the case in this example.

Hope that helps,
-Jon

Thanks Jon,

I really appreciate your help and you were right it was something about the configuration on the parents array designation.

I was able to finish one part of it and now I’m working on the other two.

Thanks for your help and also @chriddyp

Best,

1 Like