How to show Y-axis lable while using x-unified hovermode in plotly?

739 Views Asked by At

I am using x-unified hovermode in plotly with subplots. Whenever I move cursor on the plot I want to basically show x-hover info plus corresponding Y value on Y-axis at the X hover location. How can I do that? Currently Y-axis info comes

enter image description here

In above image, along with x-hover I want to see Y values highlighted on Y-axis as well. Let me know how I can achieve that!

Thanks :)

1

There are 1 best solutions below

5
Hamzah On

You can do that by adding showspikes to both axes:

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

fig = px.line(df, x="year", y="lifeExp", color="country")
fig.update_traces(mode="markers+lines", hovertemplate=None)
fig.update_layout(hovermode="x unified")

fig.update_xaxes(showspikes=True)  # <-- add this line
fig.update_yaxes(showspikes=True)  # <-- add this line

fig.show()

Output: enter image description here