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: