【发布时间】:2022-01-05 17:49:41
【问题描述】:
我想绘制 4 个块,它们的颜色随机变为白色或红色。但是,当我如下编码时,之前的颜色保持不变,直到所有四个块都是红色的。
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)
【问题讨论】:
标签: matplotlib figure real-time-updates