Interact Exercise 2

Imports


In [1]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np

In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
from IPython.html import widgets


:0: FutureWarning: IPython widgets are experimental and may change in the future.

Plotting with parameters

Write a plot_sin1(a, b) function that plots $sin(ax+b)$ over the interval $[0,4\pi]$.

  • Customize your visualization to make it effective and beautiful.
  • Customize the box, grid, spines and ticks to match the requirements of this data.
  • Use enough points along the x-axis to get a smooth plot.
  • For the x-axis tick locations use integer multiples of $\pi$.
  • For the x-axis tick labels use multiples of pi using LaTeX: $3\pi$.

In [6]:
def plot_sine1(a,b):
    x = np.arange(0.0, 12.56, 0.05)
    plt.plot(x,np.sin(a*x+b))

In [7]:
plot_sine1(5, 3.4)
plt.box(False)
plt.xlim(0,6.28);
plt.ylim(-1.0,1.0)
plt.xlabel('$X$')
plt.ylabel('$Sin(ax+b)$');
plt.xticks([0,3.14,2*3.14], ['0','$3/pi$','$6/pi$']);


Then use interact to create a user interface for exploring your function:

  • a should be a floating point slider over the interval $[0.0,5.0]$ with steps of $0.1$.
  • b should be a floating point slider over the interval $[-5.0,5.0]$ with steps of $0.1$.

In [88]:
interact(plot_sine1, a=widgets.FloatSlider(min=0.0,max=5.0,step=0.1,value=2.5), b=widgets.FloatSlider(min=-5.0,max=5.0,step=0.1,value=0.0));



In [ ]:
assert True # leave this for grading the plot_sine1 exercise

In matplotlib, the line style and color can be set with a third argument to plot. Examples of this argument:

  • dashed red: r--
  • blue circles: bo
  • dotted black: k.

Write a plot_sine2(a, b, style) function that has a third style argument that allows you to set the line style of the plot. The style should default to a blue line.


In [79]:
def plot_sine2(a, b, style=None):
    x = np.arange(0.0, 12.56, 0.05)
    if style == None:
        style = 'b-'
    else:
        style = style
    plt.plot(x,np.sin(a*x+b),style)

In [81]:
plot_sine2(4.0, -1.0, 'r--')


Use interact to create a UI for plot_sine2.

  • Use a slider for a and b as above.
  • Use a drop down menu for selecting the line style between a dotted blue line line, black circles and red triangles.

In [97]:
interact(plot_sine2, a=widgets.FloatSlider(min=0.0,max=5.0,step=0.1,value=2.5), b=widgets.FloatSlider(min=-5.0,max=5.0,step=0.1,value=0.0), style=widgets.Dropdown('b.','ko','r^'));
#This code doesn't work, but I can't figure out why, I don't really understand the error. It seems like this should be simple.


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-97-cb522857219e> in <module>()
----> 1 interact(plot_sine2, a=widgets.FloatSlider(min=0.0,max=5.0,step=0.1,value=2.5), b=widgets.FloatSlider(min=-5.0,max=5.0,step=0.1,value=0.0), style=widgets.Dropdown('b.','ko','r^'));
      2 #This code doesn't work, but I can't figure out why, I don't really understand the error. It seems like this should be simple.

/usr/local/lib/python3.4/dist-packages/IPython/html/widgets/widget_selection.py in __init__(self, *args, **kwargs)
     66         if 'options' in kwargs:
     67             self.options = kwargs.pop('options')
---> 68         DOMWidget.__init__(self, *args, **kwargs)
     69         self._value_in_options()
     70 

/usr/local/lib/python3.4/dist-packages/IPython/html/widgets/widget.py in __init__(self, *pargs, **kwargs)
    479 
    480     def __init__(self, *pargs, **kwargs):
--> 481         super(DOMWidget, self).__init__(*pargs, **kwargs)
    482 
    483         def _validate_border(name, old, new):

TypeError: __init__() takes 1 positional argument but 4 were given

In [ ]:
assert True # leave this for grading the plot_sine2 exercise