Black Lives Matter. Please consider donating to Black Girls Code today.

Compare data on hover by default

Dear Plotly’s community,
I am working with Plotly.py in a reserach project in which I need to analyze a set of data with Pandas. Everything works fine, Plotly is really awesome and I felt in love when I began to use it. However I am having a little problem for which I don’t found an answer.

I need to set the option “Compare data on hover” by default. I have seen some questions here, but no one seems useful for me. Although it may be I didn’t the find the correct one that answer my question :slight_smile:

The way in which I am creating the figure is:

fig = go.Figure()

fig.add_trace(
  go.Bar(...)
)

fig.add_trace(
  go.Bar(...)
)

fig

I tried to set hovermode="closes" using update_layout with fig. However it doesn’t set the option I need as default. I read the Python’s Figure Reference page but I don’t find something to set it by default.

Could it be set by default?

Thanks in advance for your attention!

Regards

P.S. (1) I have data in each go.Bar but I prefer to summarize the code using ....
P.S. (2) Maybe an important thing to note is that I am working with Plotly.py inside a Jupyter Notebook on Google Colaboratory.

Hi @ivanhercaz welcome to the forum! You need to set hovermode to x if you want the hover to be displayed for the two traces. For example

import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])

fig.update_layout(barmode='group', hovermode='x')
fig.show()

3 Likes

Thank you very much @Emmanuelle! You solved my problem with your answer. Now I understand better how it works. I am going to mark your answer as the solution.

Again, thank you for your help!