使用 matplotlib 和 ipywidget 的交互式图形
Interactive Graph with matplotlib and ipywidget
我正在尝试跟进一个讨论过但没有完全解决的问题,在这里:
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);
虽然滑块显示并可以修改,但没有显示图表。
我的误会在哪里?
[编辑] 现在适用于以下解决方案,结果:
当 javascript 小部件未在 jupyter 笔记本中启用时发生。
激活你安装jupyter notebook的环境
运行 这个命令:
jupyter nbextension enable --py --sys-prefix widgetsnbextension
- 你会得到这样的输出:
Enabling notebook extension jupyter-js-widgets/extension...
- Validating: OK
- 另外,运行执行此命令后不要忘记重启笔记本。
我正在尝试跟进一个讨论过但没有完全解决的问题,在这里: 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);
虽然滑块显示并可以修改,但没有显示图表。
我的误会在哪里?
[编辑] 现在适用于以下解决方案,结果:
当 javascript 小部件未在 jupyter 笔记本中启用时发生。
激活你安装jupyter notebook的环境
运行 这个命令:
jupyter nbextension enable --py --sys-prefix widgetsnbextension
- 你会得到这样的输出:
Enabling notebook extension jupyter-js-widgets/extension... - Validating: OK
- 另外,运行执行此命令后不要忘记重启笔记本。