How to use the interact method to make basic widgets

By using the interact method and setting 'x' to 1 by default we get the following points:

  1. The little widget pops up automatically, just above the plot:
# Basic interact usage with integer slider
def f(x):
plt.plot(np.arange(0,10), x*np.arange(0,10))
plt.ylim(-30,30)
interact(f, x=1)

We will get the following output:

  1. Without having to do any extra coding, except for this one very simple function, we actually get an interactive plot. This widget changes the slope of the plot automatically, based on the fact that this argument is an integer. As we change the slider, the line automatically changes, as follows:

As we change the range of the slider to a bigger or perhaps smaller range, we pass a minimum slope. Let's perform -3 and 3 with a step size of 0.5.

  1. From the output, we can see that each click on the slider is 0.5 and going between 3 and -3. The line moves for each click, as shown in the following code:
# Range & Step size
def f(x):
plt.plot(np.arange(0,10), x*np.arange(0,10))
plt.ylim(-30,30)
interact(f, x=(-3,3,0.5))

The following output shows the output for 3.0:

The following output shows the output for -3.0:

  1. Therefore, it is very easy to set up sliders that automatically fill their range. It will choose to use a slider based on the type of the argument:
# Automatically choose appropriate widget
rands = np.random.rand(100)
def f(x):
if x:
plt.plot(rands, 'b')
else:
plt.plot(rands, 'r')
interact(f, x=True)

While choosing a different function by taking interact (f, x=True), we can see that it gives a checkbox, as shown in the following output:

  1. When we uncheck this box, we can see that it switches its color, since it's caught automatically clearing the output:

  1. The interact method can also be passed as a decorator. If, for example, we insert interact and pass a string such as Title of plot, it gives a textbox, along with the string:
# interact as a decorator
@interact(x='Title of plot')
def f(x):
plt.title(x)

The preceding code produces the following output:

  1. The contents of the string can also be changed, and we can see that it automatically feeds into the plot, as shown in the following output:

  1. We can also choose multiple widgets with the interact method. So, if we have a function that is multivalued, such as a =1, b=3, we automatically get a pair of widgets:
# Multiple widgets
def f(a,b):
plt.plot(np.arange(0,10), a*np.power(np.arange(0,10), b))
plt.title("Power Law: $x=ay^b$")
interact(f, a=1, b=3)

The preceding code will give the following slider just over the plot:

By changing the index of the power law, we get the following output:

  1. Also, by changing the slope of the power law, we get the following output:

  1. The values can also be fixed. Let's say we have two argument functions, such as a=1 and b=fixed (2), as shown in the following code:
# Fixed value
def f(a,b):
plt.plot(np.arange(0,10), a*np.power(np.arange(0,10), b))
plt.title("Power Law: $x=ay^b$")
interact(f, a=1, b=fixed(2))

By using these argument functions, we get the following output:

The fixed method that we imported from ipywidgets is a method that allows you to automatically set the value of one of these arguments. We could also use an anonymous function, such as lambda, but the ipywidgets module provides an automatic way to do this without the need to use lambda functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.208.13