【问题标题】:Use Plotly and ipywidgets使用 Plotly 和 ipywidgets
【发布时间】:2019-01-02 17:59:10
【问题描述】:

我在 ipywidgets 中离线使用 plotly,但无法弄清楚为什么下面的代码不起作用:

op=widgets.Output()

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

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

如果我单独调用 display(op) 它可以工作,但我无法弄清楚为什么当我调用 display(box) 时绘图不显示...

你有什么想法吗?

【问题讨论】:

    标签: python python-3.x plotly ipywidgets vbox


    【解决方案1】:

    widget.Output() 的输出似乎只能使用一次。下面的代码创建了一个函数,可以多次调用该函数来创建输出。

    导入库

    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
    

    创建示例数据框

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

    创建函数

    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() 无框绘图

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

    display()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)
    

    【讨论】:

    • 非常感谢,这正是我想要的!
    猜你喜欢
    • 2021-10-19
    • 2017-10-04
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2018-10-22
    • 2021-02-17
    • 2017-01-12
    相关资源
    最近更新 更多