我正在尝试跟进讨论的问题,但没有完全解决,在这里: Interactive matplotlib using ipywidgets
我在 jupyter notebook 环境中使用 Python 2.7,希望在 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);
虽然 slider 显示并可以修改,但不显示任何图形。
我的误解在哪里?
最佳答案
当 jupyter notebook 中未启用 javascript 小部件时会发生这种情况。
激活您安装了 jupyter notebook 的环境。
运行这个命令:
jupyter nbextension enable --py --sys-prefix widgetsnbextension
- 你会得到这样的输出:
Enabling notebook extension jupyter-js-widgets/extension... - Validating: OK
- 另外,不要忘记在运行此命令后重新启动笔记本。
关于python - 使用 matplotlib 和 ipywidget 的交互式图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44180957/