import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 500)
plt.plot(x, x**2, label='$f(x) = x^2$')
plt.plot(x, x**4, label='$f(x) = x^4$ ')
plt.plot(x, x**6, label='$f(x) = x^6$')
plt.legend(loc='upper right', fontsize=12)
plt.show()
A color map is a collection of colors, each color corresponding to an index between 0 and 1.
import matplotlib.cm as cm
x = np.linspace(0, 1, 40)
plt.figure(figsize=(14, 1))
for t in x:
plt.plot(t, 0, 'o', ms=12, color=cm.jet(t))
plt.title('jet')
plt.show()
plt.figure(figsize=(14, 5))
plt.subplots_adjust(hspace=0.5)
plt.subplot(3, 1, 1)
for t in x:
plt.plot(t, 0, 'o', ms=12, color=cm.inferno(t))
plt.title('inferno')
plt.subplot(3, 1, 2)
for t in x:
plt.plot(t, 0, 'o', ms=12, color=cm.viridis(t))
plt.title('viridis')
plt.subplot(3, 1, 3)
for t in x:
plt.plot(t, 0, 'o', ms=12, color=cm.autumn(t))
plt.title('autumn')
plt.show()
dir(cm)
x = np.linspace(-np.pi, np.pi)
ax = plt.subplot(1, 1, 1)
ax.set_facecolor(cm.inferno(0.9))
plt.plot(x, np.sin(x), '-', color=cm.jet(0.9))
plt.plot(x, np.cos(x), '--', color=cm.winter(0.2))
plt.show()
RGB color model uses basic colors: red, green, blue. All other colors are obtained as a mixture of these basic colors. In the RGB model each color is specified by three numbers describing intensities of red, green, blue. In matplotlib these values are numbers between 0 (full off) and 1 (full on).
plt.plot(0, 0, 'o', ms=100, color=(0,1,1))
plt.show()
ax = plt.subplot(1, 1, 1)
ax.set_facecolor((0,0,0))
h = 3**0.5
plt.axis('equal')
plt.ylim(-0.5, 2.2)
plt.plot(-1, 0, 'o', ms=40, color=(1, 0,0))
plt.plot(1, 0, 'o', ms=40, color=(0, 0,1))
plt.plot(0, h, 'o', ms=40, color=(0, 1,0))
plt.plot(-1/2, h/2, 'o', ms=40, color =(1, 1, 0))
plt.plot(1/2, h/2, 'o', ms=40, color =(0, 1, 1))
plt.plot(0, 0, 'o', ms=40, color =(1, 0, 1))
plt.plot(0, h/3, 'o', ms=40, color =(1, 1, 1))
plt.show()
a = np.arange(20)
print(a)
The np.reshape()
function can be used to change the shape of a numpy array:
b= a.reshape(4, 5)
print(b)
c = a.reshape(10,2)
print(c)
b.shape
gives dimensions of a numpy array b
:
b.shape
c.shape
a.shape
x = np.arange(20)
print(x)
y = x.reshape(20,1)
print(y)
The ravel()
function unravels a multidimensional array into a one dimensional array.
z = y.ravel()
print(z)
u = x.reshape(1, 20)
print(u)
Using indexing to access array elements:
a = np.arange(20)
print(a)
b = a.reshape(4, 5)
print(b)
b[0,0]
b[3,4]
b[2, 3] = -1
print(b)
Note: Reshaping produces a view of the original array, by changing the reshaped array we change also the original:
print(a)
The functions np.zeros()
, np.ones()
, np.empty()
can be used to create arrays with more than one dimension:
d = np.zeros((3, 7), dtype='int')
print(d)
a = np.ones((3,3))
print(a)
b = 2*a
print(b)
c = np.arange(9).reshape(3,3)
print(c)
d = b+c
print(d)
e = b*c
print(e)
Matrix multiplication can be done using the dot()
function:
f = d.dot(e)
print(f)
Numpy functions work on multidimensional array:
print(np.sin(f))
a = np.arange(35).reshape(5,7)
print(a)
print(a[0, :])
print(a[:, 0])
print(a[-1, :])
print(a[2:4, :])
print(a[2:4, 3:])
a[2:4, 3:] = 0
print(a)
x = np.arange(8).reshape(2,4)
a[2:4, 3:] = x
print(a)
np.random.rand()
function produces a numpy array of random floats in the range [0, 1).
import numpy as np
a= np.random.rand(10)
print(a)
a = np.random.rand(3,3)
print(a)
The matplotlib function imshow()
creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow()
.
import matplotlib.pyplot as plt
plt.imshow(a, cmap='jet', interpolation='nearest')
plt.show()
import matplotlib.pyplot as plt
plt.imshow(a, cmap='viridis', interpolation='nearest')
plt.show()
import matplotlib.pyplot as plt
plt.imshow(a, cmap='gray', interpolation='nearest')
plt.show()
import matplotlib.pyplot as plt
plt.imshow(a, cmap='jet', interpolation='bilinear')
plt.show()
import matplotlib.pyplot as plt
plt.imshow(a, cmap='jet', interpolation='bicubic')
plt.show()
plt.imshow(a)
plt.show()
The imsave()
function is similar to imshow()
but it saves the image to a file:
a = np.random.rand(200, 200)
plt.imsave('imsave_test.png', a, cmap='jet')
a = np.arange(100).reshape(10,10)
plt.imshow(a)
plt.show()
print(a)
a = np.random.rand(3,3)
print(a)
plt.imshow(a)
plt.show()
plt.imshow(a, vmin = 0, vmax=5)
plt.show()
The function imshow() can create images with colors specified by RGB coordinates. To do this we use a 3-dimensional numpy array a of dimensions m×n×3. Such array consists of three slices a[:,:, 0], a[:,:, 1], and a[:,:, 2] that give RGB coordinates of colors in the image.
a = np.random.rand(4,4,3)
print(a)
a[2, 3, 0]
a[2,3,1]
plt.imshow(a)
plt.show()
a[0,0,0]
a[0,0,1]
a[0,0,2]
plt.figure(figsize=(15, 4))
plt.subplot(1,4,1)
plt.imshow(a)
plt.subplot(1,4,2)
r = np.copy(a)
r[:, :, 1] = 0
r[:, :, 2] = 0
plt.imshow(r)
plt.subplot(1,4,3)
g = np.copy(a)
g[:, :, 0] = 0
g[:, :, 2] = 0
plt.imshow(g)
plt.subplot(1,4,4)
b = np.copy(a)
b[:, :, 0] = 0
b[:, :, 1] = 0
plt.imshow(b)
plt.show()
b = np.zeros((20,20,3))
plt.imshow(b)
plt.show()
b[5:10, 10:15, 0] = 1
plt.imshow(b)
plt.show()
b[7:12, 7:12, 1] = 1
plt.imshow(b)
plt.show()
plt.imshow(b, interpolation='bilinear')
plt.show()
b = np.zeros((20,20,3))
plt.imshow(b)
plt.show()
og = np.array([92, 100, 40])/255
b[:, 15, 0] = og[0]
b[:, 15, 1] = og[1]
b[:, 15, 2] = og[2]
plt.imshow(b)
plt.show()
og = np.array([92, 100, 40])/255
b[:, 15, :] = og
plt.imshow(b)
plt.show()