如何在 jupyter notebook 中排列多个交互式图形?

How to arrange multiple interactive figures in a jupyter notebook?

我尝试使用 ipywidget 和 matplotlib 在 jupyter notebook 中排列多个图形,但它没有像我预期的那样工作...当我 运行 单元格 invidually 它工作正常但是当我重新 运行 整个笔记本什么都没有显示。 您可以了解一下我尝试做的事情,并通过找出我整个笔记本中的错误来帮助我。

单元格 1

import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt

%matplotlib widget

pi = np.pi
t = np.linspace(0,2*pi,100)
fig1 = plt.figure(1,figsize=(6,2))
ax1 = fig1.add_subplot(1, 1, 1, aspect=1)

def update_plot(phase,amplitude):
    ax1.clear()
    ax1.plot(t,amplitude*np.sin(t+phase), color='r', label='Sinus')
    ax1.legend(loc=0)

phase = widgets.FloatSlider(min=-2,max=2,value=0,description='Phase:')
amplitude = widgets.FloatSlider(min=0,max=8,value=1.5,description='Amplitude:')
widgets.interactive(update_plot, phase=phase, amplitude=amplitude)

单元格 2

fig2 = plt.figure(2,figsize=(6,2))
ax2 = fig2.add_subplot(1, 1, 1, aspect=1)
t2 = np.linspace(0,2*pi,100)

def update_plot(phase,amplitude):
    ax2.clear()
    ax2.plot(t2,amplitude*np.cos(t2+phase), color='b', label='Cosinus')
    ax2.legend(loc=0)

phase = widgets.FloatSlider(min=-2,max=2,value=0,description='Phase:')
amplitude = widgets.FloatSlider(min=0,max=8,value=1,description='Amplitude:')
widgets.interactive(update_plot, phase=phase, amplitude=amplitude)

整个笔记本

我不知道如何共享笔记本,所以我将 ipynb 文件粘贴到 pastebin 中。 https://pastebin.com/UdK4jzHK

要查看地块,您只需添加 图。1 fig2 等在每个图形单元格的末尾。
notebook example

PS:您可以随时在 Collab 上分享您的笔记本:https://colab.research.google.com/