【发布时间】:2022-11-04 05:46:15
【问题描述】:
使用 seaborn,我可以将两个图与下面的代码结合起来。
# LINE PLOT
r_30 = r[r['ID']==30] #choosing person 30
g = sns.FacetGrid(r_30, col="VISIT", sharex=False, sharey=False,aspect=2,col_wrap=2,legend_out=True) # 2 columns, legend is outside of plot
g.map(sns.lineplot, "DATE_TIME", "SBP",color='red',label='SBP',style=r_30['MODE']) # style/shape of line changes with mode
g.map(sns.lineplot, "DATE_TIME", "DBP",color='blue',label='DBP',style=r_30['MODE'])
g.set_axis_labels("DATE_TIME", "Blood Pressure (mmHg)")
然后,我得到下图:
我想在 Plotly 中有相同的情节,以便我可以在 Dash 中使用它。为此,我查阅了旧帖子并使用了
1)
trace1 = px.scatter(r_30, x="DATE_TIME", y="SBP", color="MODE", facet_col="VISIT")
trace2 = px.scatter(r_30, x="DATE_TIME", y="DBP", color="MODE", facet_col="VISIT")
trace1.update_xaxes(matches=None)
trace1.update_yaxes(matches=None)
trace2.update_xaxes(matches=None)
trace2.update_yaxes(matches=None)
fig = go.Figure(data = trace1.data + trace2.data)
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
Dash: A web application framework for your data.
'''),
dcc.Graph(
id='example-graph',
figure=sub_fig3
)
])
if __name__ == '__main__':
app.run_server(debug=True)
fig = go.Figure(data = trace1.data + trace2.data) 工作但没有像上面那样分开图表。我怎样才能重新安排代码,以便我有单独的情节?
我还使用了 SO 的另一种代码解决方案,但这也没有分开绘图。
sub_fig3 = make_subplots(rows=2, cols=2, shared_xaxes=True, vertical_spacing=0.02)
sub_fig3 = sub_fig3.add_trace(trace1.data[0], row=1, col=1)
sub_fig3 = sub_fig3.add_trace(trace1.data[1], row=2, col=1)
sub_fig3 = sub_fig3.add_trace(trace2.data[0], row=1,col=2)
sub_fig3 = sub_fig3.add_trace(trace2.data[1], row=2,col=2)
sub_fig3 = sub_fig3.update_layout(
xaxis_rangeslider_visible=False,
xaxis3={"anchor": "y3"},
xaxis2_rangeslider_visible=False,
)
我怎样才能像在 Seaborn 中那样在 Plotly 中获得一个情节,如上图所示?
【问题讨论】:
标签: python plotly plotly-dash