Today, we will learn
pyplot
submodule to draw the graph of a function. pyplot
is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot
function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.Using pyplot
is also a first example of the application of vectorization.
Reference: Pyplot tutorial
import numpy as np
arr = np.arange(12)+1
arr[:3]
arr[3:]
arr = arr.reshape(3,-1)
print(arr)
arr[1:,:]
arr[:,0]
dir(arr)
X = np.array(range(20)).reshape((4,-1))
print(X)
X[:]
X[1:]
X[1::]
X[::-1]
X[::2]
X[0:2,1:]
X[:,1] = np.zeros(4)
print(X)
X[0:2,1:] += 10
print(X)
Make the following 5 by 5 array, without using FOR
or WHILE
loops:
array([[0, 0, 0, 0, 0],
[0, 1, 2, 3, 0],
[0, 4, 5, 6, 0],
[0, 7, 8, 9, 0],
[0, 0, 0, 0, 0]])
import matplotlib.pyplot as plt
%matplotlib inline
import math
from math import pi # so that we can use pi directly instead of math.pi
Say if we want to plot $y = \cos(x)$ and $y = \sin(x)$, first we produce a bunch of equi-distant points using linspace
function in numpy
.
xs = np.linspace(0, 2*pi, 200)
# it generates an array of 200 points between 0 and 2*pi
cosxs = np.cos(xs) # evaluates cos at these 200 points
sinxs = np.sin(xs)
# Vectorization remark: this cos is from math module
# it cannot be directly applied to xs
math.cos(xs)
# plt is the pyplot function in the matplotlib module
# to connect the all the dots
# %matplotlib qt
plt.plot(xs, cosxs) # first one is blue
plt.plot(xs, sinxs) # second one is orange
# optional
# we could even annotate the graph we have
plt.plot(xs, cosxs, color = 'blue')
plt.plot(xs, sinxs, color = 'red')
plt.grid(True)
plt.axis('auto')
plt.annotate('local maximum of $sin(x)$', xy=(pi/2, 1), xytext=(3, 1.5),
arrowprops = dict(facecolor='black',shrink = 0.1))
plt.show() # suppress the output, similar to `drawnow` in MATLAB
Estimate $\pi$ by adding the first $100$ terms in the formula: $$\frac{\pi}{4} = 1 - \frac13 + \frac15 - \frac17+ \cdots,$$ which comes from the Taylor expansion of $\arctan(x)$.
Compare the answer to the built-in math.pi
(you want to import math
module) by the following:
estpi
to store the first $100$ approximation along the way. For example, estpi[0]
should be $4\cdot 1$, estpi[1]
should be $4\cdot \left(1 - \frac13\right)$, estpi[2]
should be $4\cdot \left(1 - \frac13 + \frac15\right)$, etc.(100,)
to represent the indices, you can use linspace
or arange
.actpi
to store the actual $\pi$ value that has the same shape with estpi
.plt
in pyplot
to plot estpi
and actpi
against the index array. Suggestion: use different color
, linewidth
, linestyle
.Reference: matplotlib.pyplot.plot