Using ipywidgets with plotly in jupyter notebook

流过昼夜 提交于 2019-12-08 01:44:35

问题


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)

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.

Thank you very much for your answers.


回答1:


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

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.



来源:https://stackoverflow.com/questions/43820691/using-ipywidgets-with-plotly-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!