I wasn't able to get @queez's answer that uses 'interact' to work today (later updated in early 2023 is below); however, the ipywidgets documentation presently includes a matplotlib example, which uses 'interactive', that I was able to take and adapt @qqueezz's solution to get it working. This seems to be a much more streamlined route to make an interactive plot.
#%matplotlib inline # from the example in the documentation. but doesn't seem necessary in current JupyterLab 3.1.11 or the classic notebook available now https://github.com/fomightez/communication_voila
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
def my_sine(x, w, amp, phi):
"""
Return a sine for x with angular frequency w and amplitude amp.
"""
return amp*np.sin(w * (x-phi))
def f( w, amp, phi):
plt.figure(2)
x = np.linspace(0, 2 * np.pi, 300)
plt.plot(x, my_sine(x, w, amp, phi), color='C0')
#plt.ylim(-5, 5)
plt.grid(True) #optional grid
plt.show()
interactive_plot = interactive(f, w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))
#output = interactive_plot.children[-1]
#output.layout.height = '450px'
interactive_plot
You can go to the repo here, launch a session by choosing the launch binder
badge to the right of 'Start with the matplotlib & widget demo as a notebook' under 'Direct links to start out in notebook mode:'.
Or click here to launch directly into that notebook via MyBinder.
UPDATE:
Later (early 2023), I found queez's code from Kapernikov: Ipywidgets with matplotlib did work with interact
in JupyterLab using ipympl
.
Not following yet what changed; however, wanted to note.