Can I do subplots, where some subplotd use graph_objects and others use plotly express ? (MIX of the 2)

fig = make_subplots(rows=2, cols=1,
specs=[[{“rowspan”: 1}],
[{“rowspan”: 1}]])

scatter1 = go.Scatter(x=df.index, y=df[‘nav’], mode=‘lines’, fill=‘tozeroy’, name=‘nav’)
fig.add_trace(scatter1, row=1, col=1)

bar = px.bar(df_w, x=df_w.index, y=list(df_w.columns))
fig.add_trace(bar, row=2, col=1)

add_trace doesn’t work unfortunately for px.bar …

Many thanks in advance for your help !

Hi,

You can’t do it directly because px.bar() returns a go.Figure object, not just the traces. But you can add the traces individually:

bar = px.bar(df_w)
for trace in bar["data"]:
    fig.add_trace(trace, row=2, col=1)

As a side note, if you are using the dataframe index as x and all columns as y, you don’t have to specify these parameters.

1 Like

Many thanks, I just didn’t how how to access the traces !

You are an absolute legend !!! Thanks again !

Just one last question please.

When I use:

bar = px.bar(df2, x=df2.index, y=list(df2.columns))

on its own, this is the figure I have:

However, when I use your solution:

bar = px.bar(df2, x=df2.index, y=list(df2.columns))
for trace in bar[“data”]:
fig.add_trace(trace, row=2, col=1)

The chart is slightly different and this is surely due to the loop used.

Any idea on how the bring exactely the same chart in the subplot ? (with the bars above each others) ?

Many thanks again !

Try this:

fig.update_layout(barmode='stack')
1 Like

Works, thanks you very much again !