1

让我首先澄清一下,我并不想像在这个问题和许多其他问题中那样生成随机游走线。我正在尝试制作一个随机游走热图,当点被重新审视时会改变颜色,就像这样

我已经能够创造出这样的静物:随机游走的结果但我想看看这个过程。

我可以让这个数字出现,如果我在每一步打印数组,我可以看到步行正在工作。但人物本身并没有动画。我的代码:

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation as anim
import numpy as np
import sys
import random

length = int(sys.argv[1])

fig = plt.figure()
ax = plt.axes(xlim=(0, length-1), ylim=(0, length-1))
arr = np.zeros((length, length), dtype = int)

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                                    ['black','green','white'],
                                                    256)
bounds=[0,0,10,10]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

im=plt.imshow(arr, interpolation='nearest',
              cmap = cmap,
              origin='lower')

x = int(np.random.random_sample() * length)
y = int(np.random.random_sample() * length)

def walk():
    global x, y
        rand = np.random.random_sample()
        if rand < 0.25 :
            if x == length - 1:
                x = 0
            else: x = x + 1
        elif rand < 0.5 :
            if x == 0:
                x = length - 1
            else: x = x - 1
        elif rand < 0.75 :
            if y == length - 1:
                y = 0
            else: y = y + 1
        else:
            if y == 0:
                y = length - 1
            else: y = y - 1
    return

def stand(arr):
    global x,y
    arr[x][y] = arr[x][y] + 1
    return arr

def animate(i):
    arr=im.get_array()
    walk()
    #print(a)
    arr = stand(arr)
    im.set_array(arr)
    return [im]

anim = anim.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

运行 Python 3.6,如您所见。

这些动画网格的视频太多了,我找不到任何答案!一定有人知道怎么做。谢谢!

4

1 回答 1

1

我在下面的函数中添加了animated=True和。我也将线路更改为.vmin=0, vmax=255,imshow()stand()arr[x][y] = arr[x][y] + 10

#!/usr/bin/env python3
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation as anim
import numpy as np
import sys
import random

length = int(sys.argv[1])

fig = plt.figure()
ax = plt.axes(xlim=(0, length-1), ylim=(0, length-1))
arr = np.zeros((length, length), dtype = int)

cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
                                                    ['black','green','white'],
                                                    256)
bounds=[0,0,10,10]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

im=plt.imshow(arr, interpolation='nearest',
        cmap = cmap, vmin=0, vmax=255,
              origin='lower', animated=True) # small changes here

x = int(np.random.random_sample() * length)
y = int(np.random.random_sample() * length)

def walk():
    global x, y
    rand = np.random.random_sample()
    if rand < 0.25 :
        if x == length - 1:
            x = 0
        else: x = x + 1
    elif rand < 0.5 :
        if x == 0:
            x = length - 1
        else: x = x - 1
    elif rand < 0.75 :
        if y == length - 1:
            y = 0
        else: y = y + 1
    else:
        if y == 0:
            y = length - 1
        else: y = y - 1
    return

def stand(arr):
    global x,y
    arr[x][y] = arr[x][y] + 1000
    return arr

def animate(i):
    global x,y
    arr=im.get_array()
    walk()
    #print(a)
    arr = stand(arr)
    im.set_array(arr)
    return [im]

anim = anim.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

然后我运行它,length = 50我得到了一个动画。在这里看到它。因此,您可能需要稍微调整一下颜色选择。

于 2017-12-08T04:49:28.483 回答