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

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

I can run the code from the question without problem and I can't remember ever having to enable anything in the command line.
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.
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.
Ya maybe. But I am sure, which version actually it doesn't support. But it works for me too in those versions.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.