I want to plot a sunburst diagram where the first ring is "population" and the second ring is subdivided into the following categories according to their weight: [num_safe_hood1, num_safe_hood2, num_safe_hood3, num_safe_hood4, num_safe_hood5]. Plotted another version that doesn't implement the aforementioned "second ring". I have included both sets of code as well as any other relevant code. I have also included the outputs. If you need anymore information please ask!
For better context this is kind of what im going for incase the explanation is confusing:
goal for how the graph should look:

import pandas as pd
import seaborn as sns
import csv
import matplotlib.pyplot as plt
# Specify the path to your CSV file
csv_file_path = 'GunOwnerSruvey.csv'
data = pd.read_csv("GunOwnerSruvey.csv")
data
import plotly.graph_objects as go
import plotly.colors
def plotSunburst(data):
total_pop = len(data)
# First ring
num_gunOwners = unionOf(data, ('va4', [1], True))
num_gunOwners_carry = unionOf(data, ('va4', [1], True), ('vb11', [1], True))
num_gunOwners_carry_disp = unionOf(data, ('vb11', [1], True), ('va4', [1], True), ('vc27', [99], False))
# Second ring
num_safe_hood1 = unionOf(data, ('v1a', [1], True))
num_safe_hood2 = unionOf(data, ('v1a', [2], True))
num_safe_hood3 = unionOf(data, ('v1a', [3], True))
num_safe_hood4 = unionOf(data, ('v1a', [4], True))
num_safe_hood8 = unionOf(data, ('v1a', [8], True))
population = ["Total Population", "Gun Owners", "Gun Owners Carry", "Gun Owners Carry Disp",
"Safe Hood 1", "Safe Hood 2", "Safe Hood 3", "Safe Hood 4", "Safe Hood 8"]
values = [total_pop, num_gunOwners, num_gunOwners_carry, num_gunOwners_carry_disp,
num_safe_hood1, num_safe_hood2, num_safe_hood3, num_safe_hood4, num_safe_hood8]
parents = ["", "Total Population", "Gun Owners", "Gun Owners Carry",
"Total Population", "Total Population", "Total Population", "Total Population", "Total Population"]
# Create a color scale using Plotly's built-in color scales
color_scale = plotly.colors.sequential.YlOrRd
colors = [color_scale[(len(population) - 1) - i] for i in range(len(population))]
# Create the sunburst chart
fig = go.Figure(go.Sunburst(
labels=population,
parents=parents,
values=values,
branchvalues="total",
hoverinfo="label+value+percent parent",
marker=dict(colors=colors) # Set the colors for each slice
))
# Customize the layout
fig.update_layout(
title="Population Subsets",
width=600,
height=600
)
# Display the chart
fig.show(renderer='iframe')
plotSunburst(data)
output to this code is: output for broken code.
here is the block of code that does work and the output:
import plotly.graph_objects as go
import plotly.colors
def plotSunburst(data):
total_pop = len(data)
num_gunOwners = unionOf(data, ('va4', [1], True))
num_gunOwners_carry = unionOf(data, ('va4', [1], True), ('vb11', [1], True))
num_gunOwners_carry_disp = unionOf(data, ('vb11', [1], True), ('va4', [1], True), ('vc27', [99], False))
num_safe_hood1 = unionOf(data, ('v1a', [1], True))
num_safe_hood2 = unionOf(data, ('v1a', [2], True))
num_safe_hood3 = unionOf(data, ('v1a', [3], True))
num_safe_hood4 = unionOf(data, ('v1a', [4], True))
num_safe_hood8 = unionOf(data, ('v1a', [8], True))
population = ["Total Population", "Gun Owners", "Gun Owners Carry", "Gun Owners Carry Disp"]
values = [total_pop, num_gunOwners, num_gunOwners_carry, num_gunOwners_carry_disp]
parents = ["", "Total Population", "Gun Owners", "Gun Owners Carry"]
# Define color for each slice
#colors = ["#ff7f0e", "#ff7f0e", "#2ca02c", "#d62728"]
#colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"]
color_scale = plotly.colors.sequential.YlOrRd
colors = [color_scale[len(population) - i] for i in range(len(population))]
#colors = ["ff7f0e", "#ff7f0e", "#2ca02c", "#d62728"]
# Create the sunburst chart
fig = go.Figure(go.Sunburst(
labels=population,
parents=parents,
values=values,
branchvalues="total",
hoverinfo="label+value+percent parent",
marker=dict(colors=colors) # Set the colors for each slice
))
# Customize the layout
fig.update_layout(
title="Population Subsets",
width=1200,
height=1200
)
# Display the chart
fig.show(renderer='iframe')
plotSunburst(data)
output for working code:

I tried changing the way color was calculated but it did nothing. Im really new to Sunbursts plots and plotly so anything helps! Even course on how to do sunbursts.