In [11]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
In [12]:
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 [93]:
t=np.linspace(0,4*np.pi,250)
def plot_sine1(a, b):
plt.figure(figsize=(6+a,6))
plt.plot(t, np.sin(t*a+b))
plt.xticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi], [0,r'$\pi$',r'$2\pi$',r'$3\pi$',r'$4\pi$'])
plt.tight_layout
plt.ylabel('sin(ax+b)')
plt.xlabel('x')
plt.title('Sin(ax+b) vs x')
plt.ylim(-1.25,1.25)
plt.yticks([-1.0,0,1.0], [-1,0,1])
In [94]:
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 [95]:
interact(plot_sine1, a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1))
Out[95]:
In [96]:
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 [97]:
def plot_sine2(a, b, style):
plt.figure(figsize=(6+a,6))
plt.plot(t, np.sin(t*a+b), style)
plt.xticks([0,np.pi,2*np.pi,3*np.pi,4*np.pi], [0,r'$\pi$',r'$2\pi$',r'$3\pi$',r'$4\pi$'])
plt.tight_layout
plt.ylabel('sin(ax+b)')
plt.xlabel('x')
plt.title('Sin(ax+b) vs x')
plt.ylim(-1.25,1.25)
plt.yticks([-1.0,0,1.0], [-1,0,1])
In [98]:
plot_sine2(4.0, -1.0, 'r--')
Use interact to create a UI for plot_sine2.
a and b as above.
In [99]:
interact(plot_sine2, a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1), style=('b','ko','r^'))
In [100]:
assert True # leave this for grading the plot_sine2 exercise
In [ ]:
In [ ]:
In [ ]: