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

Add trace to subplot created with low-level API

I am trying to add a trace to a subplot that was not created with make_subplots, but rather with the low-level api documented at the bottom of this page. However, using add_trace does not work because the subplots were not created with make_subplot, as documented here.

Is it at all possible to add a trace to a subplot if it was not created with make_subplots?

trace1 = go.Scatter(
    x=[1, 2, 3],
    y=[4, 5, 6]
)
trace2 = go.Scatter(
    x=[20, 30, 40],
    y=[50, 60, 70],
    xaxis="x2",
    yaxis="y2"
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        domain=[0, 0.7]
    ),
    xaxis2=dict(
        domain=[0.8, 1]
    ),
    yaxis2=dict(
        anchor="x2"
    )
)
fig = go.Figure(data=data, layout=layout)
fig.show()

Hi @hiram,

To add new traces use fig.add_scatter(), or fig.add_bar, i.e. fig.add_tracetype, and specify the xaxis, yaxis for each added trace. Check it, adding these traces to your fig corresponding to code posted above:

fig.add_scatter(x=[1,1.5, 2, 2.5, 3], y=[5, 4.2, 5.8, 4.75], line_color='magenta')

fig.add_scatter(x=[20, 25, 35, 40], y=[60, 53, 68, 54], line_color='green', xaxis='x2', yaxis='y2')