【问题标题】:matplotlib-problem with real-time update of figure in a loopmatplotlib问题,循环实时更新图形
【发布时间】: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


    【解决方案1】:

    通过对您提供的代码进行一些小的修改,您可以获得所需的结果。

    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)
    

    【讨论】:

    • 感谢@BrendanA 的回复;实际上并非如此。您已更改 randperm 的位置。我想生成 4 个二进制数,指示 4 个块是红色还是白色。并且在 100 次迭代的循环中,每次都会生成并绘制这 4 个数字。但我想实时更新颜色。
    • 编辑了我的答案!
    • 谢谢@BrendanA。这太棒了。编辑后的代码解决了这个问题。实际上,我正在寻找一种在运行期间不关闭绘图窗口的方法。有没有办法做到这一点?
    • 我真的不确定。我确实找到了这个包,它可能会给你这个功能吗? github.com/stsievert/python-drawnow
    • 谢谢@BrendanA。使用提供的软件包,我设法找到了答案。它现在作为新答案添加。
    【解决方案2】:

    使用@BreandanA 的指导和cmets,我设法找到了如下答案:

    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)
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多