Fancy indexing
Fancy indexing is indexing that does not involve integers or slices, which is conventional indexing. In this tutorial, we will practice fancy indexing to set the diagonal values of the Lena photo to 0
. This will draw black lines along the diagonals, crossing through them.
The following is the code for this example:
import scipy.misc import matplotlib.pyplot as plt face = scipy.misc.face() xmax = face.shape[0] ymax = face.shape[1] face=face[:min(xmax,ymax),:min(xmax,ymax)] xmax = face.shape[0] ymax = face.shape[1] face[range(xmax), range(ymax)] = 0 face[range(xmax-1,-1,-1), range(ymax)] = 0 plt.imshow(face) plt.show()
The following is a brief explanation of the preceding code:
Set the values of the first diagonal to
0
.To set the diagonal values to
0
, we need to specify two different ranges for the x and y values (coordinates in a Cartesian coordinate system):face[range(xmax), range(ymax)] =...