如何在jupyter笔记本中排列多个交互式图形?

问题描述 投票:0回答:1

我尝试使用 ipywidget 和 matplotlib 在 jupyter 笔记本中排列多个图形,但它没有像我预期的那样工作...当我单独运行单元格时,它运行良好,但当我重新运行整个笔记本时,没有任何显示。 您可以一睹我尝试做的事情,并通过查找整个笔记本中的问题来帮助我。

细胞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

matplotlib jupyter-lab ipywidgets
1个回答
0
投票

要查看绘图,您只需添加 图。1 Fig2 等位于每个图形单元格的末尾。
笔记本示例

© www.soinside.com 2019 - 2024. All rights reserved.