%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) fig, ax = plt.subplots() x = np.linspace(-5,0,100) y = x * (x - 5) ax.plot(x,y,linewidth=3,color='royalblue') x = np.linspace(0,5,100) y = x * (x - 5) ax.plot(x,y,linewidth=3,color='crimson') x = np.linspace(5,10,100) y = x * (x - 5) ax.plot(x,y,linewidth=3,color='teal') ax.grid() ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_xlim([-5,10]) ax.legend(['$x<0$','$0<x<5$','$x>5$'])
y = x * (x - 5)
,
for the function three times as we have new -values. This
is undesirable as it invites to making coding mistakes. Imagine for instance
that the function is changed. Then the code must be changed
consistently at three different places.
fig, ax = plt.subplots() for x1, x2, col in [(-5, 0, 'deepskyblue'), (0, 5, 'coral'), (5, 10, 'turquoise')]: x = np.linspace(x1,x2,100) y = x * (x - 5) ax.plot(x,y,linewidth=3,color=col) ax.grid() ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_xlim([-5,10]) ax.legend(['$x<0$','$0<x<5$','$x>5$'])
(-5, 0, 'deepskyblue')
tuple is handled, and the three
variables x1, x2, col
specified between
for
and in
assume the three values from the tuple, i.e.
-5
,
0
, and
'deepskyblue'
, respectively. Then follows the next tuple, and so on.
zip
function to create the
tuples as in the previous example. It takes the form:
fig, ax = plt.subplots() x1_vals = [-5, 0, 5] x2_vals = [0, 5, 10] colors = ['deepskyblue', 'coral', 'turquoise'] for x1, x2, col in zip(x1_vals, x2_vals, colors): x = np.linspace(x1,x2,100) y = x * (x - 5) ax.plot(x,y,linewidth=3,color=col) ax.grid() ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_xlim([-5,10]) ax.legend(['$x<0$','$0<x<5$','$x>5$'])
zip(x1_vals, x2_vals, colors)
becomes exactly (-5, 0, 'deepskyblue')
, the
next becomes (0, 5, 'coral')
, and so on
np.cos
when the NumPy package was imported
as np
. When writing a mathematical function in
Python, the shortest syntax possible involves a so-called
lambda function. Here is an example:
f = lambda x: x * (x - 5) f(1), f(np.array([1, 2]))
(-4, array([-4, -6]))
fig, axs = plt.subplots() f = lambda x: x * (x - 5) x = np.linspace(-5,0,100) axs.plot(x,f(x),linewidth=3,color='cornflowerblue') x = np.linspace(0,5,100) axs.plot(x,f(x),linewidth=3,color='tomato') x = np.linspace(5,10,100) axs.plot(x,f(x),linewidth=3,color='olive') axs.grid() axs.set_xlabel('$x$') axs.set_ylabel('$y$') axs.set_xlim([-5,10]) axs.legend(['$x<0$','$0<x<5$','$x>5$'])
fig, axs = plt.subplots() x = np.linspace(0,10,100) f1 = lambda x: x * (x - 2) f2 = lambda x: x * (x - 5) f3 = lambda x: x * (x - 7) axs.plot(x,f1(x),linewidth=3,color='royalblue') axs.plot(x,f2(x),linewidth=3,color='crimson') axs.plot(x,f3(x),linewidth=3,color='teal') axs.grid() axs.set_xlabel('$x$') axs.set_ylabel('$y$') axs.set_xlim([0,10]) axs.legend(['$x_0=2$','$x_0=5$','$x_0=7$'])
fig, axs = plt.subplots() x = np.linspace(0,10,100) f = lambda x0: lambda x: x * (x - x0) f1 = f(3) f2 = f(6) f3 = f(8) axs.plot(x,f1(x),linewidth=3,color='steelblue') axs.plot(x,f2(x),linewidth=3,color='firebrick') axs.plot(x,f3(x),linewidth=3,color='seagreen') axs.grid() axs.set_xlabel('$x$') axs.set_ylabel('$y$') axs.set_xlim([0,10]) axs.legend(['$x_0=3$','$x_0=6$','$x_0=8$'])
subplots
call. fig, axs = subplot(2,1)
for instance provides two rows
and one coloumn of axes, i.e. two axes atop each other. The
second variable, axs
, in the assignment statement
now becomes a list of Axes object instances. They can then be
addressed one by one, e.g. by invoking ax = axs[0]
and
ax = axs[1]
as in this example:
fig, axs = plt.subplots(2,1,sharex=True,figsize=(6,7.5)) fig.suptitle('Two Gaussians') f_of_x0 = lambda x0: lambda x: np.exp(-np.power(x-x0,2)) xs = np.linspace(-5,5,100) x0s = [-1, 0] ax = axs[0] x0 = x0s[0] f = f_of_x0(x0) ax.plot(xs,f(xs)) ax.grid() ax.set_ylabel('$y$') ax.set_title('$x_0={}$'.format(x0)) ax = axs[1] x0 = x0s[1] f = f_of_x0(x0) ax.plot(xs,f(xs)) ax.grid() ax.set_ylabel('$y$') ax.set_title('$x_0={}$'.format(x0)) ax.set_xlabel('$x$') fig.tight_layout() fig.subplots_adjust(top=0.9)
fig, axs = plt.subplots(4,1,sharex=True,figsize=(6,10)) fig.suptitle('Moving Gaussians') f_of_x0 = lambda x0: lambda x: np.exp(-np.power(x-x0,2)) x0s = [-1, 0, 1, 2] xs = np.linspace(-5,5,100) for ax,x0 in zip(axs,x0s): f = f_of_x0(x0) ax.plot(xs,f(xs)) ax.grid() ax.set_ylabel('$y$') ax.set_title('$x_0={}$'.format(x0)) ax.set_xlabel('$x$') fig.tight_layout() fig.subplots_adjust(top=0.9)
Choose which booklet to go to: