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 !