Home > database >  matplotlib-problem with real-time update of figure in a loop
matplotlib-problem with real-time update of figure in a loop

Time:01-08

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)

CodePudding user response:

With some small edits to the code you provided, you can get the desired result.

from numpy import random, round
from matplotlib.pyplot import figure, show, \
    close, clf, cla, ion, ioff, pause

for j in range(100):
    LDX = [100, 100, 150, 150]
    LDY = [50, 100, 50, 100]
    fig = figure()
    ax = fig.add_axes([0.125, 0.1, 0.7, 0.8])
    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')

    show(block=False)
    pause(0.5)

CodePudding user response:

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)
  •  Tags:  
  • Related