Skip to content
Advertisement

Plotly: How to add elements to hover_data using plotly.express piechart?

I am playing with examples from plotly.express piechart help page and trying to add an extra element iso_num to the hover_data property (iso_num is an int64 column in the gapminder dataframe)

JavaScript

Hovering over the slice of the pie chart then gives this:

enter image description here

where iso num value is %{customdata[1]} instead of the numeric value from the column.

What am I missing?

Thanks!

Advertisement

Answer

This seems to be a relic from back when it was stated that

Oh pie hover is a big mess

Which since seems to be have been resolved. But perhaps not for px.pie()? I’ve tried numerous approaches, but I’m only able to get the customdata + hovertemplate approach to work for go.Pie and not for px.Pie. Here’s a demonstration on how assigning values to customdata will make any variable otherwise not assigned to go.Pie() available for a custom hovertamplate:

Plot:

enter image description here

Code:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement