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
Write a plot_sin1(a, b) function that plots $sin(ax+b)$ over the interval $[0,4\pi]$.
$3\pi$.
In [24]:
def plot_sine1(a, b, step = .01):
x = np.arange(0.0, 4*np.pi, step)
y = [np.sin(a*i + b) for i in x]
xax = np.arange(0.0, 5*np.pi, np.pi)
xlabs = ["$" + str(i) + "\pi$" for i in range(5)]
plt.plot(x, y, "b-")
plt.xlim(0,np.pi*4)
plt.xticks(xax, xlabs)
plt.xlabel("x")
plt.ylabel("$\sin(" + str(round(a, 2)) + "x + " + str(round(b, 2)) + ")$")
plt.title("$\sin(" + str(round(a, 2)) + "x + " + str(round(b, 2)) + ")$")
#raise NotImplementedError()
In [25]:
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 [26]:
interact(plot_sine1, a = (0.0, 5.0, 0.1), b = (-5.0, 5.0, 0.1), step = fixed(.01));
#raise NotImplementedError()
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 [41]:
def plot_sine2(a, b, style = "b-", step = 0.01):
x = np.arange(0.0, 4*np.pi, step)
y = [np.sin(a*i + b) for i in x]
xax = np.arange(0.0, 5*np.pi, np.pi)
xlabs = ["$" + str(i) + "\pi$" for i in range(5)]
plt.plot(x, y, style)
plt.xlim(0,np.pi*4)
plt.xticks(xax, xlabs)
plt.xlabel("x")
plt.ylabel("$\sin(" + str(round(a, 2)) + "x + " + str(round(b, 2)) + ")$")
plt.title("$\sin(" + str(round(a, 2)) + "x + " + str(round(b, 2)) + ")$")
#raise NotImplementedError()
In [42]:
plot_sine2(4.0, -1.0, 'r--')
Use interact to create a UI for plot_sine2.
a and b as above.
In [43]:
interact(plot_sine2, a = (0.0, 5.0, 0.1), b = (-5.0, 5.0, 0.1), style = {"Dotted Blue Line": "b--", "Black Circles": "ko", "Red Triangles": "r^"}, step = fixed(.01));
#raise NotImplementedError()
Out[43]:
In [ ]:
assert True # leave this for grading the plot_sine2 exercise