Hover question. how to info in all trace、

我希望 当hover在某一位置,所有子图对应的数值信息都显示出来,而不是只有当前的子图显示。

  • 所有子图已经共享x轴
  • 已经设置hovermode=‘x unified’,

I hope that when hovering over a specific position, all subplots display the corresponding value information, instead of only the current subplot.

  • All subplots share the x-axis.
  • hovermode=‘x unified’ is already set.

self.figer = make_subplots(rows=nrows, cols=ncols, shared_xaxes=True,specs=specs, vertical_spacing=0.05 ) # 画布的figer
# xaxis_rangeslider_visible=True,
hovermode=‘x unified’,

@bigbai The same issue has been reported here https://community.plotly.com/t/hoversubplots-axis-not-working-with-make-subplots/84239/, BUT IT’S NOT A BUG!!!

The hoversubplots works only with subplots created via layout grid, and by specifying explicitly the xaxis to which the trace(s) in each subplot cell is/are referenced to.

Calling help(go.Layout.hoversubplots) we find out that
If hoversubplots="axis", the hover include all stacked subplots using the same axis, when hovermode is set to "x", *x unified*, "y" or *y unified*.

No let us inspect xaxis name of each trace defined viamake_sublots with shared_xaxes=True in the code by @adamschroeder, posted here https://github.com/plotly/plotly.py/issues/4603:

import plotly.graph_objects as go
from plotly import data
from plotly.subplots import make_subplots

df = data.stocks()

fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.03)
layout = dict(
    title="Stock Price Changes",
    grid=dict(rows=3, columns=1), #THIS LINE IS SUPERFLUOUS

fig.add_trace(go.Scatter(x=df["date"], y=df["AAPL"], name="Apple"), row=1, col=1)
fig.add_trace(go.Scatter(x=df["date"], y=df["GOOG"], name="Google"), row=2, col=1)
fig.add_trace(go.Scatter(x=df["date"], y=df["AMZN"], name="Amazon"), row=3, col=1)
print(fig.data[0].xaxis, fig.data[1].xaxis, fig.data[2].xaxis)

Surprise!!! The displayed axes are x x2 x3, and by help on hoversubplots="axis"
we conclude that the condition (same xaxis name) ensuring simultaneous hover of the 3 subplots is not fulfilled.
REMEMBER that setting shared_xaxes=True does not assign the same name to xaxis in all subplots.

Another drawback is that in the above code are used two methods to define subplots: 1) calling make_subplots(rows=3, cols=1)
which creates an array of subplot indices, (row, col), indexed from 1, i.e [[1,1], [2,1], [3,1]];

2) setting `grid=dict(rows=3, columns=1)`, which creates an array with indices from 0:  [[0,0], [1, 0], [2, 0]] (I'll give an example below to show that this is the indexing rule).

Setting grid in layout of a fig=make_subplots() has no effect (it is neglected by plotly.js). Commenting out the grid setting leads to the same result.

To understand how to define mixed subplots with grid, call:


Finally, an example of subplots consisting in 4 traces, two referenced to the same xaxis, in order to ensure that hoversubplots="axis" is active, and two traces referenced to non-cartesian axes (go.Pie, and go.Scatterternary). Notice the row and col starting from 0, not from 1 like in the case of make_subplots().

import plotly.graph_objects as go
import pandas as pd
from plotly import data

df = data.stocks()

    go.Scatter(x=df["date"], y=df["AAPL"], xaxis="x", yaxis="y", name="Apple"),
    go.Scatter(x=df["date"], y=df["GOOG"], xaxis="x", yaxis="y2", name="Google"),
    go.Pie(labels= ["AAPL", "GOOG"], values= [5, 2], domain=dict(row= 0, column=1)),
    go.Scatterternary(a= [2, 1, 3], b= [1, 3, 2], c= [3, 2, 1], mode= "markers")
fig.update_layout(width=750, height=550,
    title="Stock Price Changes",
    hovermode="x", #or "x unified" without dashed line
    grid = dict(rows=2, columns=2, #the grid cells (row, col): [[(0,0), (0,1)], [(1,0), (1,1)]
                subplots=[["xy", ""],  #this grid attribute gives info on the position of each trace
                          ["xy2", ""]],
    ternary=dict(domain=dict(row=1, column=1),

1 Like

That clarifies things. Thanks for sharing, @empet

Mojtaba added this axis hoversubplots issue.