Hi guy,
I just go.Sunburst (Sunburst Charts in Python) .When I try with 2 column, itβs okie. But, when I try with 3 column, It not work.
My data look like:
This is my code:
def build_hierarchical_dataframe(df, levels, value_column):
"""
Build a hierarchy of levels for Sunburst or Treemap charts.
Levels are given starting from the bottom to the top of the hierarchy,
ie the last level corresponds to the root.
"""
df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value'])
for i, level in enumerate(levels):
df_tree = pd.DataFrame(columns=['id', 'parent', 'value'])
dfg = df.groupby(levels[i:]).sum()
dfg = dfg.reset_index()
df_tree['id'] = dfg[level].copy()
if i < len(levels) - 1:
df_tree['parent'] = dfg[levels[i+1]].copy()
else:
df_tree['parent'] = 'total'
df_tree['value'] = dfg[value_column]
df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
total = pd.Series(dict(id='total', parent='',
value=df[value_column].sum()))
df_all_trees = df_all_trees.append(total, ignore_index=True)
return df_all_trees
def handle_data_sunburst_sumary_2col(dff,lst_col):
df = dff[lst_col]
df['count']=1
result = df.groupby(lst_col,as_index=False).agg({ 'count':'sum'})
return result
def sunburst_chart_sumary(dff,lst_col):
data = handle_data_sunburst_sumary_2col(dff,lst_col)
fig = go.Figure()
levels = lst_col[::-1] #levels used for the hierarchical chart
value_column = 'count'
df_all_trees = build_hierarchical_dataframe(data, levels, value_column)
fig.add_trace(go.Sunburst(
labels=df_all_trees['id'],
parents=df_all_trees['parent'],
values=df_all_trees['value'],
branchvalues='total',
textinfo='label+percent entry',
hovertemplate='<b>%{label} </b> <br> count: %{value}<br>',
maxdepth=2,
))
fig.update_layout(margin=dict(t=10, b=10, r=10, l=10))
return fig
# fig = sunburst_chart_sumary(dff,['label_SBin','HandlerID'])
fig = sunburst_chart_sumary(dff,['label_SBin','HandlerID','TesterNumber'])
Are there any solutions for this problem? Thanks .