4

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

denfromufa
  • 5,610
  • 13
  • 81
  • 138
tmo
  • 1,393
  • 1
  • 17
  • 47
  • one thing your code is missing: from ipywidgets import StaticInteract, RangeWidget, RadioWidget – denfromufa Mar 17 '15 at 22:26
  • I have changed the example to something a lot easier. – tmo Mar 18 '15 at 14:37
  • it looks like your problem is pylab, it is not recommended way – denfromufa Mar 18 '15 at 15:35
  • Um. I don't really get what the problem is? This guy [link](https://jakevdp.github.io/blog/2013/12/05/static-interactive-widgets/) has lots of working examples. I am struggling to understand the fundamental difference? – tmo Mar 18 '15 at 15:38
  • I added working example below, return fig is what was missing – denfromufa Mar 18 '15 at 15:46

1 Answers1

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]))
denfromufa
  • 5,610
  • 13
  • 81
  • 138