Histogram plot hover data not showing all columns

Hi, Iโ€™m trying to figure out how to add display data on hover in my histogram plot. I have a pandas dataframe with 3 columns: x, y, text. I plot a histogram like so:
fig_hist = px.histogram(df, y='x', x='y', orientation='h').

However, when I hover, the only data being displayed are the x and y, not the text column.

I have tried the following without any success:

  1. I tried to add hover_name='text', but it does not show up neither.
  2. I tried to add hover_data={'text': True} , but it doesnโ€™t work neither; however, setting 'x': False was able to make x disappear on hover (same for y).
  3. Lastly I tried fig_hist.update_traces(hovertemplate='x: %{y}<br> y: %{x}<br> text: %{text}'), but itโ€™s printing the actual string %{text} instead of the content. How can I make it to display the content of text, i.e. the 3rd column of the dataframe? (If I just do df['text'], then it returns the whole column)

Hi,

Starting with the last point: you need to add extra columns/data via customdata in order to access them in the hovertemplate. Here is a link to the docs using graph_objects (not express): Hover Text And Formatting

I am not 100% sure about the attempts 1 and 2, but I believe the issue is that the โ€œtextโ€ column is not added in the aggregation to create the histogram and hover_data is silently ignored. The method 2 you described works fine with scatter and bar, so you could consider doing the aggregation yourself and use px.bar instead.

Hope this helps!

1 Like

Thanks for the tips. I eventually went with px.bar and it works well!