Search code examples
pythonpython-3.xplotlyipywidgetsvbox

Use Plotly and ipywidgets


I'm using plotly offline with ipywidgets and can't figure it out why the code below is not working:

op=widgets.Output()

with op:
     iplot([{"x": [1,2,3],"y": [3,1,6]}])    

box=widgets.VBox([widget1,op]
display(box)

If I call display(op) alone it works but I can't figure it out why the plot is not displayed when I call display(box)...

Do you have any idea?


Solution

  • It seems the output of widget.Output() can be used only once. The code below creats a function that can be called to create output multiple times.

    Import libraries

    import pandas as pd
    import numpy as np
    %matplotlib inline
    import cufflinks as cf
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot 
    
    init_notebook_mode(connected=True)
    cf.go_offline()
    
    from ipywidgets import widgets
    from IPython.display import display, clear_output, Image
    from plotly.widgets import GraphWidget
    

    Create sample dataframe

    df = pd.DataFrame({'x': [1,2,3], 'y':[3,1,6]})
    df
    

    enter image description here

    Create function

    def get_out(df,xvar,yvar):
        layout={'autosize':False,'width':300,'height':300}
        op=widgets.Output()
        with op:
            w1 = df.iplot(x=xvar,y=yvar, layout=layout) 
        return op
    

    display() plot without box

    op = get_out(df,'x','y')
    display(op)
    

    enter image description here

    display() plot with VBox()

    # Call function 
    op = get_out(df,'x','y')
    widget1 = get_out(df,'y','x')
    # Create box
    box=widgets.VBox([widget1, op],layout={'border': '3px solid black','width':'55%'})
    display(box)
    

    enter image description here