✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Px.sunburst not handling None values as expected

Hi,

I am trying to create a px.sunburst output, and for some reason “None” values assigned to leaf levels are simply showing up as “None” in the resulting sunburst. In other words, I expected to see a cut-out in the outermost ring of the sunburst, but instead I see a fully drawn/populated set of rings. Note that I am reading in the contents of a .csv file.

Here is what I am doing…

import plotly
import plotly.express as px
import pandas as pd

df = pd.read_csv('CellUsageStats.csv',dtype={'classes':str,'variants':str})
print(df.head(5))

fig = px.sunburst(df, path=['library', 'families', 'classes', 'posten', 'thresholds', 'variants', 'heights', 'drives', 'speeds'], values='population')
fig.show()

plotly.offline.plot(fig, filename='sunburst.html')

Where CellUsageStats.csv looks like…

library,families,classes,posten,thresholds,variants,heights,drives,speeds,population
g1m,buf,000,a,a,1|2|3,n,12,x3,9
g1m,inv,000,a,a,1|2|3,n,18,None,1
g1m,inv,001,a,a,1|2|3,n,18,x7,1
g1m,mux,000,a,a,1|2|3,n,12,x7,3
g1m,and,000,a,a,1|2|3,n,12,x9,3

What am I doing wrong here?

Thanks!
-Chris

Hi @kehliah,

Recently I plotted the Pew Research data on the presidential vote preference, as a Sunburst, and I solved a similar issue with a trick:
Initially I set the value 0 (or any integer) for the tree nodes (sectors here) that do not have any associated value in the Pew Research data. Then I defined a text list for sunburst sectors, containing only the label for nodes without value, and both label and value, for the other sectors. Finally.
I set textinfo ='text', hoverinfo='text' in the sunburst trace definition (I used go.Sunburst, not px.sunburst). To understand why are necessary both hoverinfo and textinfo, just comment out hoverinfo='text' to see how the sunburst data are displayed.

import pandas as pd
import numpy as np
import plotly.graph_objects as go

df = pd.read_csv('https://raw.githubusercontent.com/empet/US-Presidential-Election-2020/master/Data/Education.csv')
print(df)

#Set sector colors:

colors = ['#ffffff'] + ['#aaaaaa',  '#dddddd']*2 + ['#C0223B', '#000096']*4 + ['#C0223B', '#000096']*4


def sunburst_data(df):
    if len(df.columns) != 7:
        raise valueError('Your df is not derived from Pew Research data')
       
    population_characteristic = df.columns[0]
    level1 = list(df[population_characteristic])
    n = len(level1)
    
    labels = [population_characteristic] + level1+   [df.columns[1], df.columns[3]]*n +\
             [df.columns[2], df.columns[4]]*n
    ids = [f'{k}' for k in range(1, len(labels)+1)]   
    level2_parents = []
    for k in range(2, n+2):
        level2_parents.extend([f'{k}', f'{k}'])
    parents = [''] + ['1']*n + level2_parents + [f'{k}' for k in range(n+2, 3*n+2)] 
    print(f'\n\nlabels: {labels}')
    vals  =  df.values[:, 1:5]
    temp = np.copy(vals[:, 1])
    vals[:, 1] = vals[:, 2]
    vals[:, 2]= temp
    values = [0]*(n+1) + list(vals[:, :2].flatten()) + list(vals[:, 2:].flatten())
    text = labels[:n+1] + [f'{lab}<br>{val}%' for lab, val in zip(labels[n+1:], values[n+1:])]
    print(f'\n\ntext: {text}')
       
    return go.Sunburst(labels=labels,
                       ids=ids,
                       parents=parents,
                       text=text,
                       textinfo='text',
                       hoverinfo='text',
                       values=values,
                       marker_colors=colors,
                       insidetextorientation='radial',
                       outsidetextfont_size=20
                       )

fig = go.Figure(sunburst_data(df))

fig.update_layout(title_text='Pew Research Poll on  Presidential Vote Preference', title_x=0.5,
                  font_family='Balto',
                  font_size=16,
                  height=600, width=600
                 )



The subplots displaying the vote preference of different social groups can be seen here [https://chart-studio.plotly.com/~empet/15807]