Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

SunBurst Plot on Pandas Dataframe containing NaN Values

Hello Guys,

I’m trying to get a Sunburst graph from a pandas dataframe conataining NaN values.
I’ve already prepared the data to be in the required shape.
The dataset I’m working on is the following :

Capture

when I run this line of code :
fig = px.sunburst(df,
path=[‘treatment_1’,‘treatment_2’,‘treatment_3’,“treatment_4”,“treatment_5”],
values=“Id_Patient”,
width=1000, height=800)
fig.show()

I get the following Error :
Capture1

Can you please help me with this ?

This seems to work:

print(df)
fig = px.sunburst(df, path=['treatment_1', 'treatment_2', 'treatment_3', 'treatment_4', 'treatment_5'], values='Id_Patient', branchvalues="total")
fig.write_html("./file.html")

Thank You for your reply Lcenzato, In fact I need the None areas to be empty in the graph.
Is there a way to achieve that ?

Sorry, I can’t help, I’ve the same issue:
Sunburst chart : repeated labels + missing values

Looking at your example, at a first glance, the issue seems to have been the mixed quoting.
Here the full py:

import plotly.express as px
import pandas as pd

# create a matrix of 3 list each one of 24 element
n_ls, n_el = 6, 15;
Matrix = [["" for x in range(n_el)] for y in range(n_ls)] 

#treatment_1
Matrix[0][0]  = "T";
Matrix[0][1]  = "T";
Matrix[0][2]  = "T";
Matrix[0][3]  = "T";
Matrix[0][4]  = "T";
Matrix[0][5]  = "T";
Matrix[0][6]  = "T";
Matrix[0][7]  = "T";
Matrix[0][8]  = "T";
Matrix[0][9]  = "T";
Matrix[0][10] = "T";
Matrix[0][11] = "T";
Matrix[0][12] = "T";
Matrix[0][13] = "T";
Matrix[0][14] = "T";

#treatment_2
Matrix[1][0]  = "N";
Matrix[1][1]  = "N";
Matrix[1][2]  = "N";
Matrix[1][3]  = "N";
Matrix[1][4]  = "N";
Matrix[1][5]  = "N";
Matrix[1][6]  = "R";
Matrix[1][7]  = "R";
Matrix[1][8]  = "R";
Matrix[1][9]  = "R";
Matrix[1][10] = "R";
Matrix[1][11] = "R";
Matrix[1][12] = "R";
Matrix[1][13] = "R";
Matrix[1][14] = "None";

#treatment_3
Matrix[2][0]  = "Other_trt";
Matrix[2][1]  = "Other_trt";
Matrix[2][2]  = "Other_trt";
Matrix[2][3]  = "Other_trt";
Matrix[2][4]  = "P";
Matrix[2][5]  = "None";
Matrix[2][6]  = "A";
Matrix[2][7]  = "A";
Matrix[2][8]  = "A";
Matrix[2][9]  = "A";
Matrix[2][10] = "N";
Matrix[2][11] = "Other_trt";
Matrix[2][12] = "Other_trt";
Matrix[2][13] = "None";
Matrix[2][14] = "None";

#treatment_4
Matrix[3][0]  = "N";
Matrix[3][1]  = "N";
Matrix[3][2]  = "Other_trt";
Matrix[3][3]  = "None";
Matrix[3][4]  = "None";
Matrix[3][5]  = "None";
Matrix[3][6]  = "None";
Matrix[3][7]  = "None";
Matrix[3][8]  = "Other_trt";
Matrix[3][9]  = "None";
Matrix[3][10] = "Other_trt";
Matrix[3][11] = "L";
Matrix[3][12] = "N";
Matrix[3][13] = "None";
Matrix[3][14] = "None";

#treatment_5
Matrix[4][0]  = "P";                  
Matrix[4][1]  = "None";                  
Matrix[4][2]  = "N";                  
Matrix[4][3]  = "None";                  
Matrix[4][4]  = "None";                  
Matrix[4][5]  = "None";
Matrix[4][6]  = "A";
Matrix[4][7]  = "H";
Matrix[4][8]  = "Other_trt";
Matrix[4][9]  = "None";
Matrix[4][10] = "A";
Matrix[4][11] = "E";
Matrix[4][12] = "None";
Matrix[4][13] = "None";                  
Matrix[4][14] = "None";                

#Total_Power
Matrix[5][0]  = 2;
Matrix[5][1]  = 2;
Matrix[5][2]  = 1;
Matrix[5][3]  = 3;
Matrix[5][4]  = 1;
Matrix[5][5]  = 13;
Matrix[5][6]  = 1;
Matrix[5][7]  = 1;
Matrix[5][8]  = 2;
Matrix[5][9]  = 1;
Matrix[5][10] = 1;
Matrix[5][11] = 1;
Matrix[5][12] = 1;
Matrix[5][13] = 1;
Matrix[5][14] = 29;

df = pd.DataFrame(dict(treatment_1=Matrix[0], treatment_2=Matrix[1], treatment_3
=Matrix[2], treatment_4=Matrix[3], treatment_5=Matrix[4], Id_Patient=Matrix[5]))

print(df)
fig = px.sunburst(df, path=['treatment_1', 'treatment_2', 'treatment_3', 'treatm
ent_4', 'treatment_5'], values='Id_Patient', branchvalues="total")
fig.write_html("./file.html")
1 Like

Ahh i see, well I’ve figured out how to perform it using R SunburstR package.
I still don’t know why this can’t be done in Plotly :confused:

1 Like