Create upper and lower graphs using Plotly

Asked 1 years ago, Updated 1 years ago, 307 views

"I would like to display the graph on the top and bottom two levels, but if you add ""fig.update_layout(...)"" in the bottom graph, the top graph disappears.
I would appreciate it if you could tell me how to deal with this.

import plotly.graph_objs as go
import plotly.subplots assp

# Data Preparation
x = np.array ([1, 2, 3, 4, 5])
y1 = np.array ([1,3,5,4,2])
y2 = np.array ([2, 4, 3, 1, 5])
y3 = np.array ([6, 4, 2, 5, 3])

# upper graph
config=sp.make_subplots (rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.05)
config.add_trace(go.Scatter(x=x,y=y1,mode='lines+markers',name='y1'),row=1,col=1)
config.update_yaxes(title_text='y1', range=[0,7], row=1, col=1)


# bottom graph
config.add_trace(go.Scatter(x=x,y=y2,mode='lines+markers',name='y2',yaxis="y2"),row=2,col=1)
config.add_trace(go.Bar(x=x,y=y3,name='y3'),row=2,col=1)
config.update_layout(yaxis=dict(title='y2', overlay='y2', side='left', yaxis2=dict(title='y3', side='right')))


# Viewing Graphs
config.update_layout (title='2-stage graph', height=600)
config.show()

python plotly-python

2023-03-04 09:20

1 Answers

fig.update_layout(yaxis=dict(title='y2', overlay='y2', side='left'), yaxis2=dict(title='y3', side='right')))

Remove , overlaying='y2' in and change the following to display two graphs up and down:

fig.update_layout(yaxis=dict(title='y2', side='left'), yaxis2=dict(title='y3', side='right')))

The overlaying argument appears to be used to overlap the specified axis (in this case y2).


2023-03-04 10:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.