Interactive functions and figures

In this notebook, we are going to have a look at some simple methods to make Python function interactive with the help of the ipywidgets package.

In [26]:
%pylab inline
from ipywidgets import *
Populating the interactive namespace from numpy and matplotlib

We now know how to plot a mathematical function.

In [2]:
t=linspace(0,2*pi,100);
plot(t,sin(t))
Out[2]:
[<matplotlib.lines.Line2D at 0x7f6d71ad1358>]

Let us create a function that plots a sine wave with a given frequency!

In [19]:
def freki(omega):    
    plot(t,sin(omega*t))
In [20]:
freki(2.0)

Here comes the magic. The interact() function makes our previous function interactive.

In [28]:
interact(freki,omega=(0,10,0.1));

Let us observe now how the interact() construction works. Let us now define a very simple function!

In [9]:
def func(x):
     print(x)

interact is a function that expects a function as its first parameter, and it expects the input parameters of this function as keyword arguments. It returns a widget, that can be of multiple type, but its aim is to serve the func function. It gives an input parameter to func, runs it, then waits for the user to change the state of the widget. If we gave integers in brackets to the keyword argument, we get a slider going through integer numbers between these two values.

In [10]:
interact(func,x=(0,10)); 

If we set a bool to a keyword argument, we get a checkbox.

Ha egy bool értéket adunk meg, akkor egy pipálható dobozt:

In [11]:
interact(func,x=False);

If we give an arbitrary list, we get a dropdown menu.

In [12]:
interact(func,x=['hétfő','kedd','szerda']);

If the numbers are not integers in the brackets, or at least one of them is not integer, we get a float slider.

In [14]:
interact(func,x=(0,10,0.1));

The following examples show some possible cases, even with more precise specification such as Checkbox():

In [31]:
interact(func,x=IntSlider(min=0,max=10,step=2,value=2,description='integer slider x='));
In [30]:
interact(func,x=FloatSlider(min=0,max=10,step=0.01,value=2,description='float slider x='));
In [32]:
interact(func,x=Dropdown(options=['Monday','Tuesday','Wednesday'],description='dropdown x='));
In [33]:
interact(func,x=Checkbox());
In [34]:
interact(func,x=Text());

If a function takes long to evaluate, then we may use interact_manual instead of interact. This is going to run the function only when a button is pushed.

In [35]:
interact_manual(func,x=(0,10));

A bit more on widgets can be found here. Let us now have a look at a multivariate interact!

In [36]:
t=linspace(0,2*pi,100);
def oszci(A,omega,phi,c):
    plot(t,A*sin(omega*t+phi),color=c)
    plot(pi,A*sin(omega*pi+phi),'o')
    xlim(0,2*pi)
    ylim(-3,3)
    xlabel('$t$',fontsize=20)
    ylabel(r'$A\,\sin(\omega t+\varphi)$',fontsize=20)
    grid(True)
In [38]:
interact(oszci,
            A    =FloatSlider(min=1,max=2,step=0.1,value=2,description='A'),
            omega=FloatSlider(min=0,max=10,step=0.1,value=2,description=r'$\omega$'), 
            phi  =FloatSlider(min=0,max=2*pi,step=0.1,value=0,description=r'$\varphi$'),
            c =Dropdown(options=['red','green','blue','darkcyan'],description='szín'));