Trying to customize the hover data for a plotly express sunburst plot. Been scratching my head on this one a while, trying to comb the documentation but not coming up with quite what I want. I see the post on adding percentages to the text info on the graph, but I am having trouble adding them to the hover data instead. How can we move these percentages from “label” to “hover”? I see documentation for updating plotly.graph_objects plots, but not for plotly.express.
Here is code I have so far:
if results:
#Build dataframe for sunburst from grouped results
universities = [] ; groups = [] ; counts =[]
for result in results:
universities.append(result[0])
groups.append(result[1])
counts.append(result[2])
df = pd.DataFrame(
dict(Universities=universities, Groups=groups, Counts=counts)
)
fig = px.sunburst(df, path=['Universities', 'Groups'], values='Counts', hover_name="Universities")
## Can't quite get the hover info as desired.
## Want University, Group (if applicable, won't apply to inner level), and percentage
fig.update_traces(hovertemplate = "University: %{parent}: <br>Group: %{label} </br>Count:%{value} </br>Percentage:%{percent parent}")
fig.update_traces(textinfo="label+percent parent")
return dcc.Graph(figure=fig)
Here is what the above produces:
And commenting out the first fig.update_traces(…) line:
Also, is there a way to specify a different hovertemplate for the inner level vs. the outer level(s)?