Jupyter Notebook: Interactive Story with Widgets

I am trying to create an interactive plot that depends on widgets. The problem is that when changing the parameters using the slider, the new graph is executed after the previous one, instead I expect that only one graph will change in accordance with the parameters.

Example:

from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

import matplotlib.pyplot as plt
%matplotlib inline

import numpy as np

def plot_func(freq):
    x = np.linspace(0, 2*np.pi)
    y = np.sin(x * freq)
    plt.plot(x, y)

interact(plot_func, freq = widgets.FloatSlider(value=7.5,
                                               min=1,
                                               max=5.0,
                                               step=0.5))

After moving the slider to 4.0, I have:

enter image description here

while I just want one shape to change when I move the slider. How can I achieve this?

(I am using Python 2.7, matplotlib 2.0, and I just upgraded my laptop and jupyter to the latest version. Let me know if more information is needed.)

+4
source share
2 answers

, , :

  • ; %matplotlib notebook
  • , , .

, :

%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);

enter image description here

plt.show(), .

+7

(?), jupyter / ipywidgets. , , , plt.show() plot_func.

+2

Source: https://habr.com/ru/post/1678415/


All Articles