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)