Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython Notebook, Static Interact; What am I missing?

Recently I found this post on Interactive Widgets.

I am trying to implement this in some simple code, which iterates the logistic equation, and plots the consequent timeseries:

%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

Then import the relevant packages:

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]))

But, Alas, the output is a mess. It seems to be plotting all possible combinations of r,t and x0 instead of just one.

Can anyone tell me what I'm doing wrong?

best, T

like image 375
tmo Avatar asked May 29 '26 15:05

tmo


1 Answers

%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]))
like image 179
denfromufa Avatar answered Jun 01 '26 03:06

denfromufa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!