Introducing Jupyter NotebooksΒΆ
First, set up the environment:
[1]:
import matplotlib
import matplotlib.pyplot as pl
import numpy as np
try:
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
except AttributeError:
print('Magic function can only be used in IPython environment')
matplotlib.use('Agg')
pl.rcParams["figure.figsize"] = [15, 8]
Then, define a function that creates a pretty graph:
[2]:
def SineAndCosineWaves():
# Get a large number of X values for a nice smooth curve. Using Pi as np.sin requires radians...
x = np.linspace(0, 2 * np.pi, 180)
# Convert radians to degrees to make for a meaningful X axis (1 radian = 57.29* degrees)
xdeg = 57.29577951308232 * np.array(x)
# Calculate the sine of each value of X
y = np.sin(x)
# Calculate the cosine of each value of X
z = np.cos(x)
# Plot the sine wave in blue, using degrees rather than radians on the X axis
pl.plot(xdeg, y, color='blue', label='Sine wave')
# Plot the cos wave in green, using degrees rather than radians on the X axis
pl.plot(xdeg, z, color='green', label='Cosine wave')
pl.xlabel("Degrees")
# More sensible X axis values
pl.xticks(np.arange(0, 361, 45))
pl.legend()
pl.show()
Finally, call that function to display the graph:
[3]:
SineAndCosineWaves()