So I'm trying to build a Plotly sunburst graph that displays percentParent
for each element in the graph. This works fine for all elements except for when I have only a single option for the central node/ring/whatever (see example below)
Since the central node obviously does not have a parent, it appears to bug out and display the bracketed call on percentParent
from the texttemplate
field. However, if there are 2 (or more) central nodes, it automatically calculates each's percentage of the sum total of the two.
My question is: When I have only 1 central node, how can I either hide this field for the central node only or make it correctly display "100%"?
Example code:
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({'node_names': ['Center', 'Yes', 'No'],
'node_parent': ['', 'Center', 'Center'],
'node_labels': ['Center', 'Center_Yes', 'Center_No'],
'node_counts': [1000, 701, 299]})
fig = go.Figure(
data=go.Sunburst(
ids=df["node_names"],
labels=df["node_labels"],
parents=df["node_parent"],
values=df["node_counts"],
branchvalues="total",
texttemplate = ('%{label}<br>%{percentParent:.1%}'),
),
)
fig.show()