I am following the tutorial example for sunburst diagram in plotly on python3 from the official documentation.
Based on this example, I have built my DataFrame in a manner exactly similar to the example and verified the sanity of the data.
Now, here is the issue. If I add branchvalues='total'
like shown in the following code, I get a blank white diagram. Which means, nothing gets rendered.
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
branchvalues='total',
marker=dict(
colors=df_all_trees['color'],
colorscale='RdBu'),
hovertemplate='<b>%{label} </b> <br></br> Allocated: %{value}',
name=''
))
If I remove branchvalues='total'
, like in the following code,
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
marker=dict(
colors=df_all_trees['color'],
colorscale='RdBu'),
hovertemplate='<b>%{label} </b> <br></br> Allocated: %{value}',
name=''
))
I get a rather distorted diagram.
What am I doing wrong?
EDIT - ADDED DATA AND CODE
Data
Same data in csv
,id,parent,value,color
0,Tax Planning,Astral Poly,1.54,1.54
1,Tax Planning,Avenue Super.,8.46,8.46
2,Tax Planning,Bajaj Fin.,8.75,8.75
3,Tax Planning,Bandhan Bank,2.75,2.75
4,Tax Planning,Bharti Airtel,1.74,1.74
5,Tax Planning,Cummins India,1.5,1.5
6,Tax Planning,Divi's Lab.,2.16,2.16
7,Tax Planning,Endurance Tech.,0.86,0.86
8,Tax Planning,H D F C,4.9,4.9
9,Tax Planning,HDFC Bank,6.35,6.35
10,Tax Planning,ICICI Bank,2.32,2.32
11,Tax Planning,Info Edg.(India),5.08,5.08
12,Tax Planning,Kotak Mah. Bank,8.454707583,8.454707583
13,Tax Planning,MAS FINANC SER,0.74,0.74
14,Tax Planning,Maruti Suzuki,4.71,4.71
15,Tax Planning,Motherson Sumi,1.19,1.19
16,Tax Planning,Multi Comm. Exc.,1.29,1.29
17,Tax Planning,Nestle India,4.53,4.53
18,Tax Planning,Net CA & Others,1.1,1.1
19,Tax Planning,Pidilite Inds.,5.72,5.72
20,Tax Planning,Shree Cement,1.17,1.17
21,Tax Planning,Sundaram Fin.Hol,0.04,0.04
22,Tax Planning,Sundaram Finance,1.58,1.58
23,Tax Planning,Symphony,1.76,1.76
24,Tax Planning,TCS,6.17,6.17
25,Tax Planning,TTK Healthcare,0.07,0.07
26,Tax Planning,TTK Prestige,2.07,2.07
27,Tax Planning,Tata Global,0.52,0.52
28,Tax Planning,Tech Mahindra,1.39,1.39
29,Tax Planning,Torrent Power,3.61,3.61
30,Tax Planning,V-Guard Inds.,0.55,0.55
31,Tax Planning,WABCO India,1.74,1.74
32,Astral Poly,Plastics Products,1.54,1.54
33,Avenue Super.,Trading,8.46,8.46
34,Bajaj Fin.,Finance & Investments,8.75,8.75
35,Bandhan Bank,Banks - Private Sector,2.75,2.75
36,Bharti Airtel,Telecommunications - Service Provider,1.74,1.74
37,Cummins India,Engines,1.5,1.5
38,Divi's Lab.,Pharmaceuticals - Indian - Bulk Drugs,2.16,2.16
39,Endurance Tech.,Auto Ancillaries,0.86,0.86
40,H D F C,Finance - Housing,4.9,4.9
41,HDFC Bank,Banks - Private Sector,6.35,6.35
42,ICICI Bank,Banks - Private Sector,2.32,2.32
43,Info Edg.(India),Miscellaneous,5.08,5.08
44,Kotak Mah. Bank,Banks - Private Sector,8.454707583,8.454707583
45,MAS FINANC SER,Finance & Investments,0.74,0.74
46,Maruti Suzuki,Automobiles - Passenger Cars,4.71,4.71
47,Motherson Sumi,Auto Ancillaries,1.19,1.19
48,Multi Comm. Exc.,Miscellaneous,1.29,1.29
49,Nestle India,Food - Processing - MNC,4.53,4.53
50,Net CA & Others,Net CA & Others,1.1,1.1
51,Pidilite Inds.,Chemicals,5.72,5.72
52,Shree Cement,Cement - North India,1.17,1.17
53,Sundaram Fin.Hol,Finance & Investments,0.04,0.04
54,Sundaram Finance,Finance & Investments,1.58,1.58
55,Symphony,Domestic Appliances,1.76,1.76
56,TCS,Computers - Software - Large,6.17,6.17
57,TTK Healthcare,Pharmaceuticals - Indian - Bulk Drugs & Formln,0.07,0.07
58,TTK Prestige,Domestic Appliances,2.07,2.07
59,Tata Global,Tea,0.52,0.52
60,Tech Mahindra,Computers - Software - Large,1.39,1.39
61,Torrent Power,Power Generation And Supply,3.61,3.61
62,V-Guard Inds.,Electric Equipment,0.55,0.55
63,WABCO India,Auto Ancillaries,1.74,1.74
64,Auto Ancillaries,Equity,3.7899999999999996,3.7899999999999996
65,Automobiles - Passenger Cars,Equity,4.71,4.71
66,Banks - Private Sector,Equity,19.874707583,19.874707583
67,Cement - North India,Equity,1.17,1.17
68,Chemicals,Equity,5.72,5.72
69,Computers - Software - Large,Equity,7.56,7.56
70,Domestic Appliances,Equity,3.83,3.83
71,Electric Equipment,Equity,0.55,0.55
72,Engines,Equity,1.5,1.5
73,Finance & Investments,Equity,11.11,11.11
74,Finance - Housing,Equity,4.9,4.9
75,Food - Processing - MNC,Equity,4.53,4.53
76,Miscellaneous,Equity,6.37,6.37
77,Net CA & Others,Net CA & Others,1.1,1.1
78,Pharmaceuticals - Indian - Bulk Drugs,Equity,2.16,2.16
79,Pharmaceuticals - Indian - Bulk Drugs & Formln,Equity,0.07,0.07
80,Plastics Products,Equity,1.54,1.54
81,Power Generation And Supply,Equity,3.61,3.61
82,Tea,Equity,0.52,0.52
83,Telecommunications - Service Provider,Equity,1.74,1.74
84,Trading,Equity,8.46,8.46
85,Equity,total,93.71470758299998,93.71470758299998
86,Net CA & Others,total,1.1,1.1
87,total,,94.814707583,94.814707583
The above data / csv needs to be imported in a dataframe knows as alloc
Code
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
df = alloc
average_score = df['PERC_HOLD'].sum()
fig = go.Figure()
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
branchvalues='total',
marker=dict(
colors=df_all_trees['color'],
colorscale='RdBu',
cmid=average_score),
hovertemplate='<b>%{label} </b> <br> Sales: %{value}<br> Success rate: %{color:.2f}',
name=''
))
fig.update_layout(margin=dict(t=10, b=10, r=10, l=10))
fig.write_html("fig2.html")