Sunburst Hover Data

Hello,

Within the hover data box, I would like to show a sum of some numerical data which is not related to the shape of the sunburst. However I am currently struggling to work out a way to sum up this data, see example below:

data = {'first_column':  ['first_value', 'second_value', 'third_value', 'fourth_value', 'fifth_value', 'fifth_value'],
        'second_column': ['North', 'East', 'South', 'West', 'North', 'North'],
        'third_column': [1, 1, 1, 1, 1, 1.67],
        'fourth_column':[9,9,7,6,5,4]
        }

df = pd.DataFrame(data)

fig = px.sunburst(
    data_frame=df,
    path=["first_column", "second_column"],
    color = "first_column",
    values="third_column",
    color_discrete_sequence=px.colors.qualitative.Pastel,
    maxdepth=2,
    hover_name="first_column",
    hover_data={"fourth_column":True},
    template='ggplot2',
)

Ideally, ā€œfourth columnā€ would show 9 within the hover data, however it seems to throw an error as the ā€œfifth valueā€ numbers within the ā€œfourth_columnā€ are different. Does anyone know how I might be able to get ā€œfourth columnā€ heading within the hover_data to display 9 in this case, or the sum of the ā€œfourth columnā€ values?

Ok so I see you deleted this, and that the reason was just that ā€˜fifth_valueā€™ was duplicated in the dictionary which Iā€™m sure then you caught, butā€¦

I have to say this is an instance of scary reasons why checking our results is so important! Since pandas.DataFrame still converts it and the chart still ran. What do they call those no error message errors? If it were not for the (?) showing up for the fourth_column after that, I know I might never have seen what was happening myself.

I was looking at the df and df.dtypes to try to figure out how the chart was showing 2.67 but now I understand what can happen! Thanks for this little real-life data lesson of my day!

Hi kmhurchla,

Thanks for your thoughts!

Yes it was exactly that however I wanted it to be that, I want to see the sum of the ā€˜fourth_columnā€™ values.

Ie, for the section in the sunburst diagram titled ā€˜fifth_valueā€™, I was hoping the parameter in the hover_data box called ā€˜fourth_columnā€™ would display ā€˜9ā€™, instead it displays (?). This seems to be that it doesnā€™t know which value in ā€˜fourth_columnā€™ to display - 5 or 4. If both values were say 5, the hover_data parameter called ā€˜fourth_columnā€™ would say 5.

I hope this makes sesne?

Ben

I see what youā€™re trying to do now. If you change the column from int64 to a float data type will it work?

It seems to have worked in the float column (1+1.67=2.67) so maybe 4.0+5.0 will work as well.

I was so puzzled at this being some kind of existential problem but actually if that works itā€™s an interesting idea I never thought to try. Because you had one float value in the other column pandas automatically made that column float with pd.DataFrame.

HI kmhurchla,

No unfortunately that doesnā€™t work, the reason that 2.67 shows up is because ā€˜third_columnā€™ is specified as the values within the figure creation. That is why it is summed.

I am wondering if there is a second way to specify a second set of values to sum per category, but struggling to find anything, or work out how.

Thanks!

Yep makes sense. Try a few other things to get this to show/be recognized:

  1. Using graph objects instead of express, can you force it into the hierarchy with blank parents up the chain (the center most parent is already blank)? I.e. tie the extra value to every hierarchy level with zeroes maybe (?)

Branch Values section here shows how the structure is written

  1. Try custom values in the hover. This is listed as available with px but c you can reference an example of using customdata in the hovertemplate posted by @empet earlier for a scatterplot How to selecting columns in dataframe for hovertemplate?

See the docs custom_data under parameters: plotly.express.sunburst ā€” 5.8.0 documentation

The challenge with customdata is (I think) youā€™ll have to rewrite the whole hover whereas you just want to add one value and keep the rest of the existing hover.

Iā€™ve used customdata really well in a bar chart but havenā€™t tried with sunburst. These options can be specific to chart type in my experience but give it a try.

Referencing your other post for anyone else that may read this related one:

Was this problem solved? I have basically the exact same problem now. In the most outer ring it partially works sometimes. But in inner rings the sum is always displayed by a ?

edit: nvm it never works if there is more than one line to sum