在 Jupyter Notebook 中并排显示图表

Plotly Express Graphs side by side in Jupyter Notebook

我在 Jupyter Notebook 中并排显示 Plotly.Express 图表时遇到了挑战。似乎该功能尚不可用。有没有办法使用ipywidgets,我们可以实现这个?我正在尝试使用下面的代码,其中 a、b 是要并排显示的图形对象。

import ipywidgets
from ipywidgets import HBox, Layout, widgets
from IPython.display import display
out1=widgets.Output()
out2=widgets.Output()
with out1:
    display.display(a)
with out2:
    display.display(b)
hbox=widgets.HBox([out1,out2])
hbox
  • 直接用make_subplots()
  • 实现
  • 通过使用 px 创建轨迹并将它们放置在左/右
  • 中进行演示
  • ipwidgets 是第二个解决方案
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
import ipywidgets as widgets

fig = make_subplots(rows=1, cols=2)
fig_l = px.line(
    pd.DataFrame({"x": np.linspace(1, 100, 300), "y": np.sin(np.linspace(1, 20, 300))}),
    x="x",
    y="y",
)
fig_r = px.bar(
    pd.DataFrame({"x": np.linspace(1, 20, 30), "y": np.cos(np.linspace(1, 20, 30))}),
    x="x",
    y="y",
)
fig.add_trace(
    fig_l.data[0],
    row=1,
    col=1,
)
fig.add_trace(
    fig_r.data[0],
    row=1,
    col=2,
)
fig

ip 小部件

widgets.HBox(
    [go.FigureWidget(fig_l.data, layout={"width":500, "height":300}), go.FigureWidget(fig_r.data, layout={"height":300})],
)