In [2]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
In [3]:
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 [4]:
def plot_sine1(a,b):
x=np.linspace(0,4*np.pi,100)
y=np.sin(a*x+b)
plt.figure(figsize=(9,6))
plt.plot(x,y)
plt.title('sin(ax+b)')
plt.xlim(right=4*np.pi)
plt.xticks(np.arange(0,5*np.pi,np.pi),['0','$\pi$','$2\pi$','$3\pi$','$4\pi$'])
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
In [5]:
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 [6]:
interact(plot_sine1,a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1))
Out[6]:
In [7]:
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--
bo
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 [8]:
def plot_sine2(a,b,style='b'):
x=np.linspace(0,4*np.pi,100)
y=np.sin(a*x+b)
plt.figure(figsize=(9,6))
plt.plot(x,y,style)
plt.title('sin(ax+b)')
plt.xlim(right=4*np.pi)
plt.xticks(np.arange(0,5*np.pi,np.pi),['0','$\pi$','$2\pi$','$3\pi$','$4\pi$'])
ax=plt.gca()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
In [9]:
plot_sine2(4.0, -1.0, 'r--')
Use interact
to create a UI for plot_sine2
.
a
and b
as above.
In [11]:
interact(plot_sine2,a=(0.0,5.0,0.1), b=(-5.0,5.0,0.1), style={'blue':'b--','black':'ko','red':'r^'})
In [ ]:
assert True # leave this for grading the plot_sine2 exercise