Sunday 29 October 2023

plotting subplots with a shared slider in python

I am trying to plot 3 subplots (separate plots directly one under the other) that share the same x range data with a slider that controls all of them on the bottom. I have managed to do that with one plot but I do not know how to add the other 2 subplots. I am sharing the code with a small part of the dataseries.

from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # pandas series, string
y1=[90, 92, 89, 82, 82, 78, 76, 82, 85, 88] # pandas series, numpy int64
y2=[20, 21, 19, 20, 18, 17, 14, 16, 18, 23] # pandas series, numpy int64
y3=[40, 42, 41, 42, 44, 45, 47, 49, 45, 46] # pandas series, numpy int64


fig1 = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1)
fig2 = px.scatter(x=x,y=y1,labels = dict(x = "time",y = "var"))
fig1 = go.Figure(data = fig2.data) # this is defined like that so I can add a second dataset on the same plot if needed, but I need y2,y3 on a separate plot
fig1.update_layout(yaxis_title='var',xaxis_title='time')
fig1.update_xaxes(rangeslider_visible=True)
fig1.show()

Thank you!



from plotting subplots with a shared slider in python

No comments:

Post a Comment