In [1]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import math
In [2]:
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 [13]:
def plot_sine1(a,b):
f = plt.figure()
ax = plt.subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.xticks([math.pi, 2*math.pi, 3*math.pi, 4*math.pi], [r'$\pi$',r'$2\pi$',r'$3\pi$',r'$4\pi$'])
plt.xlabel("x")
plt.ylabel("Sin(%.1fx+%.1f)"%(a,b))
plt.plot(np.linspace(0, 4*math.pi, 1000), np.sin(a*np.linspace(0, 4*math.pi, 1000)+b))
In [14]:
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 [15]:
interact(plot_sine1, a=(0.0,5.0,.1), b=(-5.0,5.0,.1))
In [40]:
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 [16]:
def plot_sine2(a,b,style='b-'):
f = plt.figure()
ax = plt.subplot(111)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.xticks([math.pi, 2*math.pi, 3*math.pi, 4*math.pi], [r'$\pi$',r'$2\pi$',r'$3\pi$',r'$4\pi$'])
plt.xlabel("x")
plt.ylabel("Sin(%.1fx+%.1f)"%(a,b))
plt.plot(np.linspace(0, 4*math.pi, 1000), np.sin(a*np.linspace(0, 4*math.pi, 1000)+b), style)
In [17]:
plot_sine2(4.0, -1.0, 'r--')
Use interact to create a UI for plot_sine2.
a and b as above.
In [19]:
interact(plot_sine2, a=(0.0,5.0,.1), b=(-5.0,5.0,.1), style={'Dotted Blue Line':'b--', 'Black Circles':'ko', 'Red Triangles':'r^'})
In [20]:
assert True # leave this for grading the plot_sine2 exercise
In [ ]: