进行数据分析之前常要引用的函式库
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
开发互动功能要引用的函式库
from ipywidgets import interact
example1:
定义好基本的function之后
def f(x):
print(x**2)
执行interact就有互动功能了
interact(f, x=2)
interact的初始value带入2.的话,就会让互动介面的最小单位变成浮点数
interact(f, x=2.)
也可以限定interact的範围
interact(f, x=(1.,10.))
example2
不同的参数,将会产生不同的interact互动介面
这个例子的参数是字串
def ShowX(x):
print(“您输入了{}”.format(x))
初始值带入”hi”
interact(ShowX,x=”hi”)
example3
def f(x):
print(x)
会产生下拉式选单(参数带入list),并印出taipei, taichung, tainan
interact(f, x=[“taipei”,”taichung”,”tainan”])
会产生下拉式选单(参数带入dictionary),并印出1, 2, 3
interact(f, x={“taipei”:1,”taichung”:2,”tainan”:3})
example4
x = np.linspace(0, 2*np.pi, 1000)
def draw(n):
y = np.sin(n*x)
设定线的粗细 = 5
plt.plot(x, y, lw=5)
在jupyter notebook裡面可不加,但是在其他的开发
介面,就要加,不然图形会出现异常
plt.show()
interact(draw,n=(1., 50.))
example5
进行数据分析之前常要引用的函式库
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
开发互动功能要引用的函式库
这裡改成import interact_manual
每次都要手动按下执行按钮,才会产生interact画面
from ipywidgets import interact_manual
定义一个简单的function做範例
def f(x):
print(x * 2)
互动的结果将会在按下执行按钮之后才会显现
interact_manual(f, x=(1., 100.))