动画Matplotlib IMShow [英] Animated matplotlib imshow

查看:121
本文介绍了动画Matplotlib IMShow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我澄清一下,我不是要像,但我想看看这个过程.

我可以显示出该图,并且如果我在每个步骤中打印阵列,都可以看到该步道正在运行.但是这个角色本身并没有动起来.我的代码:

 将matplotlib导入为mpl从matplotlib导入pyplot作为plt从matplotlib导入动画作为动画将numpy导入为np导入系统随机导入长度= int(sys.argv [1])无花果= plt.figure()ax = plt.axes(xlim =(0,长度-1),ylim =(0,长度-1))arr = np.zeros((长度,长度),dtype =整数)cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',['黑色','绿色','白色'],256)界限= [0,0,10,10]范数= mpl.colors.BoundaryNorm(bounds,cmap.N)im = plt.imshow(arr,插值='最近',cmap = cmap,origin ='lower')x = int(np.random.random_sample()*长度)y = int(np.random.random_sample()*长度)def walk():全局x,yrand = np.random.random_sample()如果rand<0.25:如果x ==长度-1:x = 0否则:x = x + 1埃利夫·兰德<0.5:如果x == 0:x =长度-1否则:x = x-1埃利夫·兰德<0.75:如果y ==长度-1:y = 0否则:y = y + 1别的:如果y == 0:y =长度-1否则:y = y-1返回def stand(arr):全局x,yarr [x] [y] = arr [x] [y] + 1返回arrdef animate(i):arr = im.get_array()走()#print(a)arr =站位(arr)im.set_array(arr)返回[im]anim = anim.FuncAnimation(无花果,动画,帧数= 200,间隔= 20,蓝调=真)plt.show() 

运行Python 3.6,如印刷品所示.

有这么多带有这些动画网格的视频,我找不到任何答案!有人必须知道该怎么做.谢谢!

解决方案

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

 #!/usr/bin/env python3将matplotlib导入为mpl从matplotlib导入pyplot作为plt从matplotlib导入动画作为动画将numpy导入为np导入系统随机导入长度= int(sys.argv [1])无花果= plt.figure()ax = plt.axes(xlim =(0,长度-1),ylim =(0,长度-1))arr = np.zeros((长度,长度),dtype =整数)cmap = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',['黑色','绿色','白色'],256)界限= [0,0,10,10]范数= mpl.colors.BoundaryNorm(bounds,cmap.N)im = plt.imshow(arr,插值='最近',cmap = cmap,vmin = 0,vmax = 255,origin ='lower',animation = True)#此处有一些小改动x = int(np.random.random_sample()*长度)y = int(np.random.random_sample()*长度)def walk():全局x,yrand = np.random.random_sample()如果rand<0.25:如果x ==长度-1:x = 0否则:x = x + 1埃利夫·兰德<0.5:如果x == 0:x =长度-1否则:x = x-1埃利夫·兰德<0.75:如果y ==长度-1:y = 0否则:y = y + 1别的:如果y == 0:y =长度-1否则:y = y-1返回def stand(arr):全局x,yarr [x] [y] = arr [x] [y] + 1000返回arrdef animate(i):全局x,yarr = im.get_array()走()#print(a)arr =站位(arr)im.set_array(arr)返回[im]anim = anim.FuncAnimation(无花果,动画,帧数= 200,间隔= 20,蓝调=真)plt.show() 

然后我用 length = 50 来运行它,然后得到一个动画.在此处中查看.因此,您可能需要稍微尝试一下颜色选择.

Let me first clarify that I'm not trying to generate random walk lines like in this and many other questions. I'm trying to make a random walk heat map that changes color as points are revisted, like this.

I've been able to create still-lifes like this: but I want to see the process.

I can get the figure to show up, and if I print the array at each step I can see that the walk is working. But the figure itself doesn't animate. My code:

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()

Running Python 3.6, as you can see by the print.

There are so many videos with these animated grids and I can't find any answers! Somebody must know how to do it. Thanks!

解决方案

I added animated=True and vmin=0, vmax=255, in the imshow() function below. I also changed the stand() line to 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()

And I ran it with length = 50 and I get an animation. See it here. So you may have to play around with your color choices a bit.

这篇关于动画Matplotlib IMShow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆