I am trying to create a sunburst chart for plotly, however when I run it with fig.show(), it opens a completely empty page with no error message anywhere? Code i'm using is below, thanks for any help I can get.
import plotly.graph_objects as go
fig = go.Figure(go.Sunburst(
labels=['Biology', 'Economics', 'Physics', 'Chemistry', 'Psychology', 'Sociology', 'Medicine', 'Social_psychology', 'Geology', 'Mathematics', 'Classical_mechanics', 'Ecology', 'Fishery', 'Engineering', 'Molecular_biology', 'Computer_science', 'Biochemistry', 'Fisheries_management', 'Quantum_electrodynamics'],
parents=['Science_and_Health', 'Business_and_Law', 'Science_and_Health', 'Science_and_Health', 'Science_and_Health', 'Humanities_and_Social_Science', 'Science_and_Health', 'Humanities_and_Social_Science', 'Humanities_and_Social_Science', 'Business_and_Law', 'Technology', 'Humanities_and_Social_Science', 'Humanities_and_Social_Science', 'Technology', 'Science_and_Health', 'Technology', 'Science_and_Health', 'Humanities_and_Social_Science', 'Science_and_Health'],
values=[40, 37, 31, 26, 20, 19, 19, 18, 18, 16, 15, 12, 12, 11, 11, 10, 10, 10, 10]
))
fig.update_layout(margin=dict(t=0, l=0, r=0, b=0))
fig.show()
The reason your current code isn't displaying is because your parent categories have no parents themselves. You need to prepend your list of parent categories to the labels and prepend the same number of empty strings to the parents class. For sunburst charts, an empty string is considered the root.
For your example, it would look like this:
import plotly.graph_objects as go
fig = go.Figure(go.Sunburst(
labels=['Business_and_Law', 'Technology', 'Humanities_and_Social_Science', 'Science_and_Health', 'Biology', 'Economics', 'Physics', 'Chemistry', 'Psychology', 'Sociology', 'Medicine', 'Social_psychology',
'Geology', 'Mathematics', 'Classical_mechanics', 'Ecology', 'Fishery', 'Engineering', 'Molecular_biology', 'Computer_science', 'Biochemistry', 'Fisheries_management', 'Quantum_electrodynamics'],
parents=['', '', '', '', 'Science_and_Health', 'Business_and_Law', 'Science_and_Health', 'Science_and_Health', 'Science_and_Health', 'Humanities_and_Social_Science', 'Science_and_Health', 'Humanities_and_Social_Science', 'Humanities_and_Social_Science',
'Business_and_Law', 'Technology', 'Humanities_and_Social_Science', 'Humanities_and_Social_Science', 'Technology', 'Science_and_Health', 'Technology', 'Science_and_Health', 'Humanities_and_Social_Science', 'Science_and_Health'],
values=[0, 0, 0, 0, 40, 37, 31, 26, 20, 19, 19, 18, 18,
16, 15, 12, 12, 11, 11, 10, 10, 10, 10]
))
fig.update_layout(margin=dict(t=0, l=0, r=0, b=0))
fig.show()
I gave the values for the parent categories all 0, but likely you may want to implement those values as sum of the subcategories below them or some other number that makes more sense for your data.
I hope this helps!