linspace
function in NumPy. linspace
takes
three arguments, a starting value, an ending value, and the number of
evenly distributed values from the starting to the ending value. Here
we ask for 10 values from 0 to :
theta = np.linspace(0,np.pi,10) theta
array([0. , 0.34906585, 0.6981317 , 1.04719755, 1.3962634 , 1.74532925, 2.0943951 , 2.44346095, 2.7925268 , 3.14159265])
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) fig, ax = plt.subplots() theta = np.linspace(0,np.pi,10) r = 2 x = r * np.cos(theta) y = r * np.sin(theta) ax.plot(x,y,linewidth=3,color='c') ax.grid() ax.set_aspect('equal') ax.set_xlabel('$x$') ax.set_ylabel('$y$')
theta = np.linspace(0,np.pi,100) r = 1 x = r * np.cos(theta) y = r * np.sin(theta) ax.plot(x,y,linewidth=3,color='k') ax.legend(['$r=1$','$r=2$']) fig
fill
function, where we provide the -points as
one runs around the perimeter of the desired polygon. One problem
with this is that if we provide points on the outer half-circle in
anti-clockwise order, then we have to provide the points on the inner
half-circle in clockwise order. Thus the -values must be
supplied in opposite order. That can be done in several ways. A
cryptic one:
theta[::-1]
array([3.14159265, 2.7925268 , 2.44346095, 2.0943951 , 1.74532925, 1.3962634 , 1.04719755, 0.6981317 , 0.34906585, 0. ])
linspace
are interchanged:
theta = np.linspace(np.pi,0,10) theta
array([3.14159265, 2.7925268 , 2.44346095, 2.0943951 , 1.74532925, 1.3962634 , 1.04719755, 0.6981317 , 0.34906585, 0. ])
a1 = np.linspace(0,2,3) a2 = np.linspace(10,12,3) a1,a2
(array([0., 1., 2.]), array([10., 11., 12.]))
+
-operator
you get the elementwise sum:
a3 = a1 + a2 a3
array([10., 12., 14.])
concatenate
function:
a4 = np.concatenate((a1,a2)) a4
array([ 0., 1., 2., 10., 11., 12.])
fig, ax = plt.subplots() r = 2 theta = np.linspace(0,np.pi,100) x1 = r * np.cos(theta) y1 = r * np.sin(theta) r = 1 theta = np.linspace(np.pi,0,100) x2 = r * np.cos(theta) y2 = r * np.sin(theta) x = np.concatenate((x1,x2)) y = np.concatenate((y1,y2)) ax.fill(x,y,linewidth=3,facecolor='pink',edgecolor='k') ax.grid() ax.set_aspect('equal') ax.set_xlabel('$x$') ax.set_ylabel('$y$')
Choose which booklet to go to: