有两个矩阵在每次迭代时都会发生变化,我能以动画的方式显示这些变化吗?(python)



我有一段代码:

import numpy as np
from operator import xor
import matplotlib.pyplot as plt
i=input('Give the number of rows ')
j=input('Give the number of columns ')
k=input('Give number of iterations ')
M=np.random.randint(2,size=(i, j))
N=np.random.randint(2,size=(i, j))
print('Initial matrix:')
print(M)
for z in range(k):
    for x in range(i):
        if((z%2)==0):
            for y in range(j):
                if(y==0):
                    if(M.item(x,y+1)==0):
                        N[x][y]=0
                    else:
                        N[x][y]=1
                elif(y==(j-1)):
                    if(M.item(x,y-1)==0):
                        N[x][y]=0
                    else:
                        N[x][y]=1
                else:
                    N[x][y]=xor(M[x][y-1],M[x][y+1])
        else:
            for y in range(j):
                if(y==0):
                    if(N.item(x,y+1)==0):
                        M[x][y]=0
                    else:
                        M[x][y]=1
                elif(y==(j-1)):
                    if(N.item(x,y-1)==0):
                        M[x][y]=0
                    else:
                        M[x][y]=1
                else:
                    M[x][y]=xor(N[x][y-1],N[x][y+1])
if((k%2)==0): 
    print('Matrix after processing:')
    print(M)
    plt.subplot(212)
    plt.imshow(M, cmap='Greys', interpolation='nearest')
    plt.show()
else:
    print('Matrix after processing:')
    print(N)
    plt.subplot(212)
    plt.imshow(N, cmap='Greys', interpolation='nearest')
    plt.show()

这是我对第90条规则的看法(我不会谈论它,因为这对我的问题无关紧要)。我创建了2个矩阵,并在迭代时更改它们(奇数次迭代时更改一个,但偶数次更改)。我只是想展示我的矩阵是如何改变的。我试着自己去找,但没有成功。如果有人帮我,那就太好了。

您应该捕捉到的错误是,绘制的if语句一直向左,所以不是缩进循环的部分,所以它们只运行一次。您还应该在matplotlib中查找轴和图形与当前轴之间的差异,我已经将其放入一个小函数中,以完成重复的工作。

import numpy as np
from operator import xor
import matplotlib.pyplot as plt
def plotMN(M, N, describe):
    fig = plt.figure()
    Max = fig.add_subplot(121)
    print(describe)
    #print(M)
    plt.imshow(M, cmap='Greys', interpolation='nearest')
    Nax = fig.add_subplot(122)
    plt.imshow(N, cmap='Greys', interpolation='nearest')
    plt.show()
    plt.close()

i=input('Give the number of rows ')
j=input('Give the number of columns ')
k=input('Give number of iterations ')
M=np.random.randint(2,size=(i, j))
N=np.random.randint(2,size=(i, j))
plotMN(M, N, 'Initial matrices M, N:')
for z in range(k):
    for x in range(i):
        if((z%2)==0):
            for y in range(j):
                if(y==0):
                    if(M.item(x,y+1)==0):
                        N[x][y]=0
                    else:
                        N[x][y]=1
                elif(y==(j-1)):
                    if(M.item(x,y-1)==0):
                        N[x][y]=0
                    else:
                        N[x][y]=1
                else:
                    N[x][y]=xor(M[x][y-1],M[x][y+1])
        else:
            for y in range(j):
                if(y==0):
                    if(N.item(x,y+1)==0):
                        M[x][y]=0
                    else:
                        M[x][y]=1
                elif(y==(j-1)):
                    if(N.item(x,y-1)==0):
                        M[x][y]=0
                    else:
                        M[x][y]=1
                else:
                    M[x][y]=xor(N[x][y-1],N[x][y+1])
    plotMN(M, N, 'M, N after %d iterations'%z)

最新更新