Issue
I want to plot 4 blocks where their colors are randomly changed to white or red. However when I code as below, the previous colors remain unchanged until all the four blocks are red.
from numpy import random, round
from matplotlib.pyplot import figure, show, \
close, clf, cla, ion, ioff, pause
LDX = [100, 100, 150, 150]
LDY = [50, 100, 50, 100]
fig = figure()
ax = fig.add_axes([0.125, 0.1, 0.7, 0.8])
ion()
for ii in range(100):
randperm = round(random.rand(len(LDX)))
for i in range(len(LDX)):
ldx = LDX[i]
ldy = LDY[i]
h11, = ax.plot(
[ldx-25, ldx+25, ldx+25,
ldx-25, ldx-25],
[ldy+25, ldy+25, ldy-25,
ldy-25, ldy+25],
color='k')
for i in range(len(LDX)):
if randperm[i]==1:
ldx = LDX[i]
ldy = LDY[i]
h12, = ax.fill(
[ldx-25, ldx+25, ldx+25,
ldx-25, ldx-25],
[ldy+25, ldy+25, ldy-25,
ldy-25, ldy+25],
color='r')
fig.show()
fig.canvas.draw()
fig.canvas.flush_events()
pause(0.001)
Solution
Using the guidance and comments by @BreandanA, I managed to find the answer as follow:
ion()
fig = figure()
for j in range(100):
LDX = [100, 100, 150, 150]
LDY = [50, 100, 50, 100]
randperm = round(random.rand(len(LDX)))
clf()
ax = fig.add_axes([0.125, 0.1, 0.7, 0.8])
for i in range(len(LDX)):
ldx = LDX[i]
ldy = LDY[i]
h11, = ax.plot(
[ldx-25, ldx+25, ldx+25,
ldx-25, ldx-25],
[ldy+25, ldy+25, ldy-25,
ldy-25, ldy+25],
color='k')
for i in range(len(LDX)):
if randperm[i]==1:
ldx = LDX[i]
ldy = LDY[i]
h12, = ax.fill(
[ldx-25, ldx+25, ldx+25,
ldx-25, ldx-25],
[ldy+25, ldy+25, ldy-25,
ldy-25, ldy+25],
color='r')
draw_all()
pause(0.001)
Answered By - rgholizadehr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.