anim = FuncAnimation(fig=fig, func=animate, interval=100, blit=True, repeat=False)
def the_straightforward_way(arr,k):
m,n = np.shape(arr)
for i in range(m):
for j in range(n):
if arr[i,j] == k:
arr[i,j] = 1
else:
arr[i,j] = 0
return arr
def the_boolean_way(arr,k):
arr = arr == k
arr = np.array(arr,dtype = int)
return arr
import numpy as np
arr1 = np.zeros((5,4,3),dtype = int)
print(arr1)
np.shape(arr1)
arr2 = np.zeros((4,20),dtype=int)
arr2
np.transpose(arr2)
np.transpose(arr1)
np.shape(np.transpose(arr1,axes=(1,0,2)))
np.shape(arr1)
arr3 = np.ones((5,20),dtype=int)
arr3
arr2
np.concatenate([arr2,arr3])
np.concatenate([arr2,arr3],axis=1)
np.concatenate([np.zeros((3,3)),np.ones((3,3))],axis=1)
np.concatenate([np.zeros((3,3,1)),np.ones((3,3,1))],axis=2)
red = np.ones((10,10),dtype=float)
import matplotlib.pyplot as plt
green = np.ones((10,10),dtype=float)
blue = np.ones((10,10),dtype=float)
arr = np.zeros((20,20,3),dtype=float)
arr[8:18,5:15,0] = red
arr[2:12,2:12,1] = green
arr[2:12,8:18,2] = blue
plt.imshow(arr)
plt.show()
arr = np.zeros((20,20,3),dtype=float)
arr[6:20,:,0] = np.random.random((14,20))
arr[:15,:14,1] = np.random.random((15,14))
arr[:15,6:20,2] = np.random.random((15,14))
plt.imshow(arr)
plt.show()
string1 = "Hello, World!"
string2 = 'How are you?'
string1[5:8]
string2[-1]
string1+' '+string2
string1.upper()
string2.lower()
string1
string2.strip(',.?!')
string1.strip(',.?!')
string2
string3 = string1+' '+string2
string3.split()
pattern = "B14 K6 B6 K6 B6 K32 OG32 K6 OG32 K32 B32 K6 B6 K6 B32 K32 OG32 K6 OG32 K32 B6 K6 B6 K6 B28"
pattern.split()
alphanum = "fhf9939jfq0jdk02ihnd9047tnao7h438vndaoi784"
alpha = ''
num = ''
for char in alphanum:
if char.isdigit():
num += char
else:
alpha += char
print(alpha)
print(int(num))
"25"+"67"
int("25")+int("67")
colors = {"B" : [52, 80, 100], "K" : [16, 16, 16], "OG" : [92, 100, 40]}
colors.keys()
colors.values()
colors.items()
colors["K"]
colors_MacKusick = {"RB" : [0, 0, 140],"G" : [0, 120, 0],"DR" : [140, 0, 0],"K" : [0, 0, 0],"LN" : [255, 255, 255],"P" : [100, 0, 140]}
Exercise: Write a detailed plan for how you are going to generate the array of horizontal or vertical striples.
Gunn_pattern = "DG4 DB24 DG2 K24 DG24 R4"
Gunn_colors = {"DG" : [0,100,0],"DB" : [28,0,112],"K" : [16,16,16],"R" : [200,0,0]}