tfv
tfv

Reputation: 6259

Interactive Graph with matplotlib and ipywidget

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

Upvotes: 3

Views: 3097

Answers (1)

Yaman Ahlawat
Yaman Ahlawat

Reputation: 507

  • 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.

Upvotes: 3

Related Questions