在Jupyter Notebook中将ipywidgets与plotly一起使用 [英] Using ipywidgets with plotly in jupyter notebook

查看:602
本文介绍了在Jupyter Notebook中将ipywidgets与plotly一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jupyter笔记本中使用plotly的离线绘图,并想通过使用来自ipywidgets的小部件来操纵或重绘该绘图. 不幸的是,我无法适当地更新地块:

I want to use the offline plotting of plotly inside a jupyter notebook and want to manipulate or redraw the plot by using widgets from ipywidgets. Unfortunately I do not manage to update the plots appropiately:

from ipywidgets import widgets, HBox, Output
import plotly as py
from plotly.offline import iplot
from IPython.display import display

%matplotlib inline
ip_widget = widgets.FloatSlider(
    value=6,
    min=3,
    max=10,
    step=1,
    description='num',
    continuous_update = True
)

ow = Output()
def response(change):
    with ow:
        iplot([{'x':list(range(int(ip_widget.value))), 'y': list(range(int(ip_widget.value)))}])
ip_widget.observe(response)
display(ip_widget)

提供的代码有两个缺点: 它多次绘制图形. 如果使用了滑块,则仅显示图形. 我该如何克服这两个问题?请注意,我不想使用plotly的在线绘图功能,也不想使用交互来解决此问题.

The provided code has two disadvantages: It plots the graph multiple times. The graph only shows up, if the slider is used. How can I overcome these two issues? Please note that I don't want to use the online plotting capabilities of plotly and I don't want to solve this problem using interact.

非常感谢您的回答.

推荐答案

您是否尝试过使用interact?这是一个非常清楚的示例: http://nbviewer.jupyter.org/github/yankev/test/blob/master/plotlywidget_working2.ipynb

Have you tried using interact? Here is a very clear example: http://nbviewer.jupyter.org/github/yankev/test/blob/master/plotlywidget_working2.ipynb

顺便说一句,interact函数位于ipywidgets中(不再是IPython.html.widgets).除此之外,该示例是最新的.

By the way, the interact function lives in ipywidgets (and not IPython.html.widgets anymore). Other than that, the example is pretty much up-to-date.

这篇关于在Jupyter Notebook中将ipywidgets与plotly一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆