Search code examples
pythonplotlydata-visualizationplotly-pythonsunburst-diagram

Plotly: Add parallel features in sunburst with sizes proportionate to parent class


In a plotly sunburst chart, I want to add multiple child classes to a parent class. I have tried assigning values to names, parents, values as shown here. But it returns a blank figure. I tried making a path variable (in the code below) but I am not sure how to name multiple direct children to a single parent class. Here is what I want-

enter image description here

And here is the code to reproduce-

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'continent':['SA','SA','NA','NA'],
                  'country':['Brazil','Uruguay','USA','Canada'],
                  'total_vaccinations':[100,80,400,200],
                  'people_vaccinated':[40,20,100,80],
                  'people_fully_vaccinated':[8,4,11,11]})

px.sunburst(df, path=['continent','country'],values='total_vaccinations')

I want people_vaccinated and people_fully_vaccinated features as children to the respective countries depicting total_vaccinations. And I want them to be proportional in size according to their value. Help would be much appreciated!


Solution

  • As you have seen, there are two ways to create sunburst plots with px.sunburst(). Either using the path variable or using the names, parents and values variables.

    The first variant with the path variable is suitable for rectangular data, and thus not suitable for you, because you have missing values. Although there is a possibility for rectangular data with missing values, it requires that the parents of missing values do not have other entries (plotly ref).

    Thus, you have to go with the second variant. As you have some leaves with identical names, you need in addition to names, parents and values also to define a list ids with unique strings.

    import pandas as pd
    import plotly.express as px
    
    
    data = {
      'ids':['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV Brazil', 'PV Brazil', 'PFV Uruguay', 'PV Uruguay', 'PFV USA', 'PV USA', 'PFV Canada', 'PV Canada'],
      'labels': ['SA', 'NA', 'Brazil', 'Uruguay', 'USA', 'Canada', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV', 'PFV', 'PV'],
      'parent': ['', '', 'SA', 'SA', 'NA', 'NA', 'Brazil', 'Brazil', 'Uruguay', 'Uruguay', 'USA', 'USA', 'Canada', 'Canada'],
      'value': [0, 0, 100, 80, 400, 200, 8, 40, 4, 20, 11, 100, 11, 80]
      }
    
    fig =px.sunburst(data, names='labels', parents='parent',  values='value', ids='ids')
    fig.show()
    

    The result will look like this: sunburst graph