I am trying to draw these pretty boxes but instead of drawing them vertically, I want each box to overwrite the previous, so it looks like one box with changing colours every .5 seconds
I am using Jupyter notebooks and Python 3.6.
I have read about 50 similar questions and answers but can't get it to display as desired. Anyone who can help please?
from PIL import Image
import random
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow, show, clf
sq_size = 20
num_squares = 5
im_size = sq_size * num_squares
im = Image.new('RGB', (im_size, im_size), color = 'white')
pix = im.load()
#Create new colourful boxes 4 times
for _ in range(4):
startx, starty = 0, 0
#move to the right place to plot each small square
for i in range(num_squares):
startx += sq_size
for j in range(num_squares):
starty += sq_size
rshade = np.random.randint(0, 256)
gshade = np.random.randint(0, 256)
bshade = np.random.randint(0, 256)
#plot each small square
for x in range(sq_size):
for y in range(sq_size):
value = (rshade, gshade, bshade)
pix[(startx + x) % im_size, (starty + y) % im_size] = value
plt.imshow(im)
plt.show()
time.sleep(.5)
Currently this plots as follows...
but I want each box to overwrite the previous, so it looks like one box with changing colours every .5 seconds

plt.show()outside the for loop at the endanimateto create an animation