In [4]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
In [5]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
Write a plot_sin1(a, b) function that plots $sin(ax+b)$ over the interval $[0,4\pi]$.
$3\pi$.
In [31]:
def plot_sine1(a,b):
x = np.arange(0, 4*np.pi,.01)
plt.plot(x, np.sin(a*x + b))
plt.xlim(0,4*np.pi)
plt.ylim(-1.1, 1.1)
plt.xlabel("x")
plt.xticks(np.arange(0,5)*np.pi, ['0','$\pi$', '$2\pi$', '$3\pi$', '$4\pi$'])
plt.ylabel("$f(x)$")
plt.title("$f(x) = \sin(ax+b)$")
# there's got to be a more slick way to paste labels
In [26]:
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 [32]:
interact(plot_sine1, a = (0.,5.,.1), b=(-5.,5.,.1));
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:
r--bok.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 [34]:
def plot_sine2(a,b,style):
x = np.arange(0, 4*np.pi,.01)
plt.plot(x, np.sin(a*x + b),style)
plt.xlim(0,4*np.pi)
plt.ylim(-1.1, 1.1)
plt.xlabel("x")
plt.xticks(np.arange(0,5)*np.pi, ['0','$\pi$', '$2\pi$', '$3\pi$', '$4\pi$'])
plt.ylabel("$f(x)$")
plt.title("$f(x) = \sin(ax+b)$")
In [42]:
plot_sine2(4.0, -1.0, 'r--')
mem = {'x':"apple", 'y':"peach"}
mem
Out[42]:
Use interact to create a UI for plot_sine2.
a and b as above.
In [53]:
interact(plot_sine2, a = (0.,5.,.1), b=(-5.,5.,.1), \
style = {"dotted blue line":'b.', "black circles":'ko', "red triangles":"r2"});
In [ ]:
assert True # leave this for grading the plot_sine2 exercise