3

I am trying to follow up on a problem discussed, but not completely solved, here: Interactive matplotlib using ipywidgets

I am using Python 2.7 in a jupyter notebook environment and want the following function to be modified interactively in jupyter.

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
line, = ax.plot(x, np.sin(x))

def update(w = 1.0):
    line.set_ydata(np.sin(w * x))
    fig.canvas.draw()

interact(update);

While the slider shows and can be modified, no graph is shown.

Where is my misunderstanding?

[EDIT] Works well now with the solution below, result: enter image description here

tfv
  • 6,016
  • 4
  • 36
  • 67

1 Answers1

3
  • it happens when javascript widget is not enabled in the jupyter notebook.

  • activate the environment where you have installed jupyter notebook.

  • run this command:

    • jupyter nbextension enable --py --sys-prefix widgetsnbextension
  • you will get an output something like this:

Enabling notebook extension jupyter-js-widgets/extension... - Validating: OK

  • also, don't forget to restart the notebook after running this command.
Yaman Ahlawat
  • 477
  • 6
  • 17
  • I can run the code from the question without problem and I can't remember ever having to enable anything in the command line. – ImportanceOfBeingErnest May 25 '17 at 13:32
  • After the update from ipywidgets (5.1.0) and widgetsnbextension (1.1.0) this error started coming. Earlier, we used `%matplotlib inline`. But to use `%matplotlib notebook` (for the interactive purpose) you need javascript widget to be enabled. Or you can also use cufflinks for interactive plots. – Yaman Ahlawat May 25 '17 at 13:36
  • Ok, so I have ipywidgets 6.0 and widgetsnbextension 2.0. Maybe in those versions you don't need to activate anything manually for it to work. – ImportanceOfBeingErnest May 25 '17 at 13:41
  • Ya maybe. But I am sure, which version actually it doesn't support. But it works for me too in those versions. – Yaman Ahlawat May 25 '17 at 14:51
  • That's it! I had run across that command already and tried it, but probably something went wrong with the restart of notebook process. It is also necessary to manually execute the cell again after the notebook opens in order to start interaction. – tfv May 25 '17 at 16:05