iPython Notebook,Static Interact;我错过了什么?

时间:2015-03-17 11:47:28

标签: python matplotlib widget ipython ipython-notebook

最近我在Interactive Widgets上找到了this post

我试图在一些简单的代码中实现它,它迭代逻辑方程,并绘制随后的时间序列:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import pylab
from IPython.html.widgets import interact

plt.close('all')

def logiter(r, x0, t):
    y = []
    x = x0
    for i in range(t):
        x = r*x*(1-x)
        y.append(x)

    fig, plt.plot(y)
    return fig

然后导入相关的包:

from ipywidgets import StaticInteract, RangeWidget, RadioWidget

StaticInteract(logiter,
               r=RadioWidget([1, 2, 4]),
               t=RangeWidget(1, 10, 1),
               x0=RadioWidget([0.1, 0.3, 0.7]))
但是,唉,输出是一团糟。它似乎正在绘制r,t和x0的所有可能组合,而不仅仅是一个。

谁能告诉我我做错了什么?

最好的, Ť

1 个答案:

答案 0 :(得分:2)

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
#from IPython.html.widgets import interact

#plt.close('all')

def logiter(r, x0, t):
    y = []
    x = x0
    fig=plt.figure()
    for i in range(t):
        x = r*x*(1-x)
        y.append(x)

    plt.plot(y)
    return fig
from ipywidgets import StaticInteract, RangeWidget, RadioWidget

StaticInteract(logiter,
               r=RadioWidget([1, 2, 4]),
               t=RangeWidget(1, 10, 1),
               x0=RadioWidget([0.1, 0.3, 0.7]))