Using ipywidgets with plotly in jupyter notebook

2019-07-16 23:05发布

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条回答
小情绪 Triste *
2楼-- · 2019-07-17 00:09

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.

查看更多
登录 后发表回答