matplotlibjupyter-labipywidgets

How to arrange multiple interactive figures in a jupyter notebook?


I try to arrange multiple figures in a jupyter notebook using ipywidget and matplotlib but it's not working as I expect to... When I run cells invidually its working well but when I rerun the whole notebook nothing shows. You can have a glimpse of what I try to do and help me by finding what's wrong in my whole notebook.

Cell 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)

Cell2

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)

Solution

  • To see plots you can just add fig1 fig2 etc at the end of each figure cell.