Bel*_*_gr 6 python interactive widget jupyter-notebook
我的目标是让 Jupyter 笔记本中的一个单元格显示多个交互式小部件。具体来说,我想要四个滑块来裁剪图像,然后另一个单独的滑块来旋转这个裁剪后的图像。当然,当我运行代码时,应该显示两个图。这是我所拥有的。
def image_crop(a,b,c,d):
img_slic=frame[a:b,c:d]
plt.figure(figsize=(8,8))
plt.imshow(img_slic,cmap='RdBu')
return a,b,c,d
interactive_plot = interactive(image_crop, a = widgets.IntSlider(min=0,max=2000,step=10,value=500,description='Vertical_Uppper'),
b = widgets.IntSlider(min=0,max=2000,step=10,value=500,description='Vertical_Lower'),
c = widgets.IntSlider(min=0,max=1000,step=10,value=500,description='Horizontal_Left'),
d = widgets.IntSlider(min=0,max=1000,step=10,value=500,description='Horizontal_Right') )
interactive_plot
def image_rot(i):
img_rot=scipy.ndimage.rotate(frame_slic.T,i)
plt.figure(figsize=(8,8))
plt.imshow(img_rot,cmap='RdBu')
return i
interactive_plot_2 = interactive(image_rot, i =
widgets.IntSlider(min=-180,max=180,step=1,value=0,description='Rotation'))
Run Code Online (Sandbox Code Playgroud)
我可以将它放在两个单元格中(第一个单元格进行裁剪,第二个单元格旋转),但不能放在一个单元格中。
小智 6
Jupyter 将只显示一个小部件,因为它始终只显示单元格中最后一个命令的输出,因此您希望将这两个小部件放在一个命令中。您可以使用Layout来完成此操作,例如Box:
from ipywidgets import Box
items = [interactive_plot, interactive_plot_2]
box = Box(children=items)
box # <- this one command displays all children
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |