Two stacked area plots on the same chart

Hey yโ€™all :raising_hand_man:

Plotting assets

Iโ€™m plotting $MSTR assets as follows

fig = px.area(df_all, x='end', y='val', color='fact', title=f'{symbol.upper()} : Balance sheet', width=1000, height=600)

fig.add_trace(go.Scatter(x=df_assets['end'], y=df_assets['val'], mode='lines', name='Assets'))

st.plotly_chart(fig)

Plotting liabilities

Similarly, Iโ€™m plotting liabilities like this (these show up as negative):

fig = px.area(df_all_liabilities, x='end', y='val', color='fact', title=f'{symbol.upper()} : Balance sheet', width=1000, height=600)

fig.add_trace(go.Scatter(x=df_liabilities['end'], y=df_liabilities['val'], mode='lines', name='Liabilities'))

st.plotly_chart(fig)

Question

Is there a way to have both of these on the same chart?

Thanks!

I am not sure if you can do it with px.area but you can with go.Scatter

d = {'val': {0: 3, 1: 3, 2: 4, 3: 5, 4: 6, 5: 10, 6: 7, 7: 20},
     'val1': {0: -3, 1: -3, 2: -4, 3: -5, 4: -6, 5: -10, 6: -7, 7: -20},
     'year': {0: 2017, 1: 2018, 2: 2019, 3: 2020, 4: 2021, 5: 2022, 6: 2023, 7: 2024}}

df = pd.DataFrame(d)

# first plt
fig = go.Scatter(x=df['year'], y=df['val'], fill='tozeroy')
# second plt
fig1 = go.Scatter(x=df['year'], y=df['val1'], fill='tozeroy')

# create a go.Figure object
final_fig = go.Figure()
# add the traces
final_fig.add_trace(fig)
final_fig.add_trace(fig1)

1 Like

Thank you, @PyGuy :+1::+1: