Hover question. how to info in all trace、

@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(
    hoversubplots="axis",
    title="Stock Price Changes",
    hovermode="x",
    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)
fig.update_layout(layout)
fig.show()
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:

help(go.layout.Grid)

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()

fig=go.Figure([
    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,
                  showlegend=False,
    hoversubplots="axis",
    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", ""]],
                xgap=0.1, 
                ygap=0.2),
    ternary=dict(domain=dict(row=1, column=1),
                 aaxis_title_text="A",
                 baxis_title_text="B",
                 caxis_title_text="C"
                ))

1 Like