简体   繁体   English

使用matplotlib和ipywidget的交互式图

[英]Interactive Graph with matplotlib and ipywidget

I am trying to follow up on a problem discussed, but not completely solved, here: Interactive matplotlib using ipywidgets 我正在尝试跟进已讨论但尚未完全解决的问题,在这里: 使用ipywidgets的交互式matplotlib

I am using Python 2.7 in a jupyter notebook environment and want the following function to be modified interactively in jupyter. 我在jupyter笔记本环境中使用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);

While the slider shows and can be modified, no graph is shown. 尽管显示并可以修改滑块,但没有显示图形。

Where is my misunderstanding? 我的误会在哪里?

[EDIT] Works well now with the solution below, result: [EDIT]现在可以使用以下解决方案,效果很好: 在此处输入图片说明

  • it happens when javascript widget is not enabled in the jupyter notebook. 如果在jupyter笔记本中未启用javascript小部件,则会发生这种情况。

  • activate the environment where you have installed jupyter notebook. 激活您安装了jupyter笔记本电脑的环境。

  • run this command: 运行以下命令:

    • jupyter nbextension enable --py --sys-prefix widgetsnbextension
  • you will get an output something like this: 您将得到类似以下的输出:

Enabling notebook extension jupyter-js-widgets/extension... - Validating: OK 启用笔记本扩展jupyter-js-widgets / extension ...-验证:确定

  • also, don't forget to restart the notebook after running this command. 另外,不要忘记在运行此命令后重新启动笔记本计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM