Cross line through subplots

Hi everyone. How do you create cross line through subplots? I atteched example in the post below.

Hi @Sank95, I think the only way to do this is using annotations. It’s going to be cumbersome, I fear.

If you combine add_hline() to a go.layout.Shape() it’s not so tedious, see below example:
It will be easier if you define your xaxis ranges though.

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

fig = make_subplots(rows=1, cols=2, horizontal_spacing=0)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[40, 50, 60]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Line across subplots",
                  yaxis2={"side": "right"}, xaxis=dict(range=[0,3.5]),
                  xaxis2=dict(range=[15,45]), showlegend=False)

fig.add_hline(y=55)
fig.add_shape(
    go.layout.Shape(
        type="line",
        yref="paper",
        xref="x",
        x0=3.5,
        y0=45,
        x1=3.5,
        y1=55,
        line=dict(color="black", width=4),
    ),row=1,col=1)

fig.show()

2 Likes