ipywidgets是已集成到anaconda中的一款非常简单好用的交互控件。
本文以三个常用的例子展示ipywidgets的使用,更为详细的用法可参考官网ipywidgets
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
简单交互图
def func(a, b, color, title, text):
x = np.linspace(0,10,10)
plt.plot(a*x+b, c=color)
if title:
plt.title(text)
widgets.interactive(func, a=[1,2,3], b=(100,200,0.5), color=["r", "b"], title=True, text="interactive fig")

利用播放器绘制动态图
play = widgets.Play(
value=50,
min=0,
max=100,
step=1,
description="Press play",
disabled=False
)
def func(b):
x = np.linspace(0,10,100)
plt.plot(np.sin(x+b/10.0))
plt.title(f"b is {b}")
plt.show()
widgets.interactive(func, b=play)

为播放器添加进度条
play = widgets.Play(
value=50,
min=0,
max=100,
step=1,
description="Press play",
disabled=False
)
slider = widgets.IntProgress()
widgets.jslink((play, 'value'), (slider, 'value'))
ui = widgets.HBox([play, slider])
out = widgets.interactive_output(func, {"b":play})
display(ui, out)

Output()
参考
Jupyter交互控件
Jupyter notebook最简原型界面设计 - ipywidgets与lineup_widget
ipywidgets
Jupyter Widget
|