Matplotlib 图未使用 ipywidgets 滑块更新

joh*_*ohn 5 python data-visualization matplotlib jupyter-notebook ipywidgets

我有以下代码来生成一个简单的图表。

%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))
    plt.show()

interact(update)
Run Code Online (Sandbox Code Playgroud)

代码生成的图很好 - 输出

但是当我拖动滑块时,图形不会更新。关于为什么会这样的任何想法吗?

Jay*_*tel 0

这会有所作为。

# load the interactive tool
from ipywidgets import interact, interactive, widgets, fixed
try:
    from ipywidgets import Layout
except:
    pass 

import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook

def fta(freq = 50.0):
    "showing sine frequency"
    y = np.sin(freq*x)
    f, ax1 = plt.subplots(nrows=1,figsize=(8,6))
    ax1.plot(x[0:100], y[0:100],'b')
    ax1.set_ylim(ymin=-1.1, ymax=1.1)
    ax1.grid();
    # then use it interactively,
interactive( fta, freq=(0.0,100.0))
Run Code Online (Sandbox Code Playgroud)

互动情节