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


: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 [3]:
def plot_sine1(a,b):
    #style graph
    plt.figure(figsize=(10,5))
    plt.rc('xtick', labelsize=14)
    plt.rc('ytick', labelsize=12)
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    
    #Set X(input array) and Y(output array)
    x = np.linspace(0.0, 4*np.pi, 500)
    y = np.sin(a*x+b)
    
    #Label Axis/ Set Ticks
    plt.xlabel("X", fontsize = 14)
    plt.ylabel("Y", fontsize = 14)
    plt.title("y(x) = sin(%sx + %s)" %(a, b), fontsize=16)
    plt.xticks(np.linspace(0.0, 4*np.pi, 5), [r'$0$', r'$\pi$', r'$2\pi$', r'$3\pi$', r'$4\pi$'])
    plt.plot(x, y)

In [4]:
plot_sine1(5, 3.4)


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 [5]:
interact(plot_sine1, a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1));



In [6]:
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 [10]:
def plot_sine2(a, b, style='b-'):
    #Style Graph
    plt.figure(figsize=(10,5))
    plt.rc('xtick', labelsize=14)
    plt.rc('ytick', labelsize=12)
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    
    #Set x(input array) and y(output array)
    x = np.linspace(0.0, 4*np.pi, 500)
    y = np.sin(a*x+b)
    
    #More styling (Labels)
    plt.xlabel("X", fontsize = 14)
    plt.ylabel("Y", fontsize = 14)
    plt.title("y(x) = sin(%sx + %s)" %(a, b), fontsize=16)
    plt.xticks(np.linspace(0.0, 4*np.pi, 5), [r'$0$', r'$\pi$', r'$2\pi$', r'$3\pi$', r'$4\pi$'])
    
    # Now we include a style argument! 
    plt.plot(x, y, style)

In [11]:
plot_sine2(4.0, -1.0, 'b-')


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 [13]:
interact(plot_sine2, a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1), style= {"blue dotted line": 'b.', "black circles": 'ko', "red triangles": 'r^'});



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

Used "Lev Levitsky"'s idea from StackOverFlow to set tick numbers

Used "unutbu"'s method for using latex in matplotlib


In [ ]: