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

How to show horizontal line only in first subplot

I’ve made two subplots and i want to show a horizontal line on the first subplot. But currently it’s shown on the second one. How can i change that? Here’s my code:

    df_volume = DataFrame(market_data["total_volumes"], columns=["DateTime", "Volume"])
    df_volume['DateTime'] = pd.to_datetime(df_volume['DateTime'], unit='ms')
    volume = go.Scattergl(
        x=df_volume.get("DateTime"),
        y=df_volume.get("Volume"),
        name="Volume"
    )

    df_price = DataFrame(market_data["prices"], columns=["DateTime", "Price"])
    df_price['DateTime'] = pd.to_datetime(df_price['DateTime'], unit='ms')
    price = go.Scattergl(
        x=df_price.get("DateTime"),
        y=df_price.get("Price"),
        yaxis="y2",
        name="Price",
        line=dict(
            color=('rgb(22, 96, 167)'),
            width=2
        )
    )

    title = f"Price of {symbol} in {vs_cur.upper()} for past {days} days"

    # TODO: How to show the shape only on first graph?

    data = [price, volume]
    layout = go.Layout(
        yaxis=dict(domain=[0, 0.20]),
        yaxis2=dict(domain=[0.25, 1]),
        title=title,
        height=600,
        width=800,
        legend=dict(orientation='h', yanchor='top', xanchor='center', y=1, x=0.5),
        shapes=[{
            'type': 'line',
            'x0': market_data["prices"][0][0],
            'y0': market_data["prices"][len(market_data["prices"])-1][1],
            'x1': market_data["prices"][len(market_data["prices"])-1][0],
            'y1': market_data["prices"][len(market_data["prices"])-1][1],
            'line': {
                'color': 'rgb(50, 171, 96)',
                'width': 1,
                'dash': 'dashdot'
            }
        }],
    )

    fig = go.Figure(data=data, layout=layout)

Hi @Endogen,

You can can control which axes a shape is attached to using the xref and yref properties under layout.shape. See https://plot.ly/python/reference/#layout-shapes-items-shape-xref.

I can’t tell how you made your subplot, but try setting xref to 'x' and 'x2' and yref to 'y' and 'y2' until it does what you want :slightly_smiling_face:

Hope that helps!
-Jon

Awesome! Thanks a lot, that worked :smiley:

1 Like