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

5
  • one thing your code is missing: from ipywidgets import StaticInteract, RangeWidget, RadioWidget Commented Mar 17, 2015 at 22:26
  • I have changed the example to something a lot easier. Commented Mar 18, 2015 at 14:37
  • it looks like your problem is pylab, it is not recommended way Commented Mar 18, 2015 at 15:35
  • Um. I don't really get what the problem is? This guy link has lots of working examples. I am struggling to understand the fundamental difference? Commented Mar 18, 2015 at 15:38
  • I added working example below, return fig is what was missing Commented Mar 18, 2015 at 15:46

1 Answer 1

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]))
Sign up to request clarification or add additional context in comments.

4 Comments

Great! I knew i had missed something simple.
before you were plotting every time to the same plot, plt.figure() fixes this
ipywidgets.__version__==1.4.0 no longer has StaticInteract, RangeWigdet, and RadioWidget. The replacement ipywidgets.IntSlider() and ipywidgets.RadioButton() don't appear to map well.
This is different ipywidget, search for static ipywidget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.