I am trying to have two subplots share the x-axis and have them separated out by Type
which would look something like this below:
FYI
- I made two dataframes
A_df
andB_df
by 'Type' to give you an idea of what I want to do, but you're welcome to usefinal_df
! - I colored weekend as grey for a reason and would like to keep them.
Here's the reproducible code:
rng = pd.date_range('2022-04-09', periods=20, freq='D')
first_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
first_df['Type'] = 'A'
second_df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})
second_df['Type'] = 'B'
final_df = pd.concat([first_df,second_df]).sort_values(by = 'Date')
final_df['Is_Weekend'] = np.where((final_df['Date'].dt.weekday == 5), 1, 0 )
A_df = final_df[final_df['Type']=='A']
B_df = final_df[final_df['Type']=='B']
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Scatter(x=A_df['Date'], y=A_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)
fig.update_xaxes(showgrid=False)
fig.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = 'rgba(0,0,0,0)')
fig.add_trace(go.Scatter(x=A_df['Date'],
y = A_df['Val'],
line_color = 'orange',
mode = 'lines+markers',
showlegend = False),
secondary_y = False)
fig.show()
fig2 = make_subplots(specs=[[{"secondary_y": True}]])
fig2.add_trace(go.Scatter(x=B_df['Date'], y=B_df['Is_Weekend'],
fill = 'tonexty', fillcolor = 'rgba(128,128,128, 0.2)',
line_shape = 'hv', line_color = 'rgba(0,0,0,0)',
showlegend = False
),
row = 1, col = 1, secondary_y=True)
fig2.update_xaxes(showgrid=False)
fig2.update_layout(yaxis2_range=[-0,0.1], yaxis2_showgrid=False, yaxis2_tickfont_color = 'rgba(0,0,0,0)')
fig2.add_trace(go.Scatter(x=B_df['Date'],
y = B_df['Val'],
line_color = 'blue',
mode = 'lines+markers',
showlegend = False),
secondary_y = False)
fig2.show()
Edit:
How to change the order to legends?