I was delighted to see that the newer JupyterLab seemed to have much better support for ipywidgets lately and so when I saw this question at StackOverflow, I thought it was a good time to test how the solution proposed was working. In examining the current answer to this question, I was finding the answer wasn't working in repositories where I had ipywidgets enabled, even when I tried in JupyterLab.
However, the ipywidgets documentation includes a matplotlib example (found by searching 'matplotlib' in the documentation).
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
def f(m, b):
plt.figure(2)
x = np.linspace(-10, 10, num=1000)
plt.plot(x, m * x + b)
plt.ylim(-5, 5)
plt.show()
interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
I was able to take that and adapt @queezz's solution to get it working.
(Note when I did this, going to @queezz's repo here and launching a binder session and opening the MatplotliInteractTest.ipynb apparently meant to accompany, the answer, didn't allow the plot to work either. It said I suspect locking in the versions wasn't the best choice?)
#%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
interactive(children=(IntSlider(value=5, description='w', max=10), FloatSlider(value=2.0, description='amp', m…