python - 使用 matplotlib 和 ipywidget 的交互式图形

标签 python matplotlib ipython jupyter-notebook ipywidgets

我正在尝试跟进讨论的问题,但没有完全解决,在这里: 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 显示并可以修改,但不显示任何图形。

我的误解在哪里?

[编辑] 现在适用于以下解决方案,结果: enter image description here

最佳答案

  • 当 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/

相关文章:

python - 用于更改多个变量的高效 if-elif

python - 如何恢复已删除的容器

python-3.x - 如何在循环 pandas 数据帧时标记 matplotlib 散点图中的数据点?

python - Matplotlib 中 Facecolors 使用的标准化颜色图

memory-leaks - IPython.parallel 模块中的内存泄漏?

python - 如何在 ipython notebook 中显示图表

当我按下 tab (./) 时 Python 自动完成目录

python - asyncio.await失败,并出现TypeError : cannot unpack non-iterable coroutine object

Matplotlib:带有边缘颜色但没有面部颜色的颜色图的散点图

python - 如何在IPython笔记本中连续显示表格后跟数字而不在末尾出现重复的数字?