更新矩阵子图在for循环中不起作用。

huangapple go评论73阅读模式
英文:

Updating matrices subplots in for loop not working

问题

我在这里阅读了很多答案和教程,但我无法让这段代码工作。
我有一个带有三个子图的图形,都在一行上。每个子图显示一个矩阵。我编写了一个for循环,以在每次迭代中使用不同的numpy数组更新矩阵的可视化。
图形打开,显示初始状态,不更新并关闭。

import matplotlib.pyplot as plt
from matplotlib import cm
import time
import numpy as np

cmap_veg_wat = cm.get_cmap("Greens").copy()
cmap_veg_wat.set_under('#409fff')

plt.ion()

fig, ax = plt.subplots(1, 3, num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
index = count()
ind = 0

MATR1 = np.load('erb.npy')
MATR2 = np.load('carv.npy')
MATR3 = np.load('vegwat.npy')

ax[0].imshow(MATR2, cmap='Reds')
ax[1].imshow(MATR1, cmap='Greens')
ax[2].imshow(MATR3, cmap=cmap_veg_wat, vmin=0)

for x in range(3):
    npErb = 'erb' + str(x) + '.npy'
    npCarv = 'carv' + str(x) + '.npy'
    npVW = 'vegwat' + str(x) + '.npy'

    MATR1 = np.load(npErb)
    MAMTR2 = np.load(npCarv)
    MATR3 = np.load(npVW)

    x_time = np.load('xT.npy')
    y_erbpopulation = np.load('yE.npy')
    y_carvpopulation = np.load('yC.npy')
    y_vegdensity = np.load('yV.npy')

    ax[0].imshow(MATR2, cmap='Reds')
    ax[1].imshow(MATR1, cmap='Greens')
    ax[2].imshow(MATR3, cmap=cmap_veg_wat, vmin=0)

    fig.canvas.draw()
    fig.canvas.flush_events()
    time.sleep(0.1)
英文:

I read a lot of answers here and tutorials, but I cannot get this code to work.
I have a figure with three subplots on one row. Each subplot shows a matrix. I wrote a for loop to update the visualization of the matrices with a different numpy array at every iteration.
The figure opens, shows an initial state, does not update and closes.


import matplotlib.pyplot as plt
from matplotlib import cm
import time
import numpy as np

cmap_veg_wat = cm.get_cmap("Greens").copy()
cmap_veg_wat.set_under('#409fff')

plt.ion()

fig, ax = plt.subplots(1, 3, num=None, figsize=(10,10), dpi=80, facecolor='w', edgecolor='k')
index = count()
ind = 0

MATR1 = np.load('erb.npy')
MATR2 = np.load('carv.npy')
MATR3 = np.load('vegwat.npy')

ax[0].imshow(MATR2, cmap='Reds')
ax[1].imshow(MATR1, cmap='Greens')
ax[2].imshow(MATR3, cmap=cmap_veg_wat, vmin=0)

for x in range(3):
    npErb = 'erb'+ str(x) + '.npy'
    npCarv = 'carv'+ str(x) + '.npy'
    npVW = 'vegwat'+ str(x) + '.npy'

    MATR1 = np.load(npErb)
    MAMTR2 = np.load(npCarv)
    MATR3 = np.load(npVW)

    x_time = np.load('xT.npy')
    y_erbpopulation = np.load('yE.npy')
    y_carvpopulation = np.load('yC.npy')
    y_vegdensity = np.load('yV.npy')

    
    ax[0].imshow(MATR2, cmap='Reds')
    ax[1].imshow(MATR1, cmap='Greens')
    ax[2].imshow(MATR3, cmap=cmap_veg__wat, vmin=0)

    fig.canvas.draw()
    fig.canvas.flush_events()
    time.sleep(0.1)

答案1

得分: 0

I think that, mutatis mutandis, this is what you want.

I'm sorry I cannot post the animation, but if you save my code to a script, e.g. 3matrices.py and next execute it, $ python3 3matrices.py, then you'll see the animation unfold in front of you...

import matplotlib.pyplot as plt
import numpy as np
import time

cmaps = 'Reds Greens Blues'.split()
norm = plt.Normalize(vmin=0, vmax=240)

plt.ion()

fig, axs = plt.subplots(1, 3, figsize=(10,10), sharey='all',
                        dpi=80, edgecolor='k', linewidth=3)

for x in range(4):
    fig.suptitle(str(x+1))
    mx = np.arange(60).reshape(15, 4)*(x+1)
    for ax, cmap in zip(axs, cmaps):
        ax.imshow(mx, cmap=cmap, norm=norm)
    fig.canvas.draw()
    fig.canvas.flush_events()
    if x<3 : time.sleep(1.0)

input('Press Invio to exit')
英文:

I think that, mutatis mutandis, this is what you want.

I'm sorry I cannot post the animation, but if you save my code to a script, e.g. 3matrices.py and next execute it, $ python3 3matrices.py, then you'll see the animation unfold in front of you...

import matplotlib.pyplot as plt
import numpy as np
import time

cmaps = &#39;Reds Greens Blues&#39;.split()
norm = plt.Normalize(vmin=0, vmax=240)

plt.ion()

fig, axs = plt.subplots(1, 3, figsize=(10,10), sharey=&#39;all&#39;,
                        dpi=80, edgecolor=&#39;k&#39;, linewidth=3)

for x in range(4):
    fig.suptitle(str(x+1))
    mx = np.arange(60).reshape(15, 4)*(x+1)
    for ax, cmap in zip(axs, cmaps):
        ax.imshow(mx, cmap=cmap, norm=norm)
    fig.canvas.draw()
    fig.canvas.flush_events()
    if x&lt;3 : time.sleep(1.0)

input(&#39;Press Invio to exit&#39;)

huangapple
  • 本文由 发表于 2023年7月20日 22:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定