2017-12-08 13 views
0

먼저 내가 this 및 기타 많은 질문 에서처럼 임의의 보행 줄을 생성하려고하지 않는다는 것을 분명히하자. this처럼 포인트가 수정되면 색상이 바뀌는 무작위 도보 히트 맵을 만들려고합니다.Animated matplotlib imshow

다음과 같이 스틸 라이프를 생성 할 수있었습니다 : results of random walk하지만 프로세스를보고 싶습니다.

나는 그림을 볼 수 있으며 각 단계마다 배열을 인쇄하면 걸음이 작용하고 있음을 알 수 있습니다. 그러나 그림 자체는 움직이지 않습니다. 내 코드 :

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이 실행 중입니다.

애니메이션이 적용된 그리드가 너무 많아서 답변을 찾을 수 없습니다. 누군가 그것을하는 법을 알아야합니다. 감사!

답변

1

아래의 imshow() 기능에서 animated=Truevmin=0, vmax=255,을 추가했습니다. 또한 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으로 실행하고 애니메이션을 얻습니다. 그것을 here보십시오. 그래서 당신은 약간의 색 선택으로 놀아야 만 할 것입니다.

+0

정말 이상합니다. 나는 그 변화와 함께 그것을 실행하고 여전히 블랙 박스를보고있다 ... –

+0

나는'arr [x] [y] = arr [x] [y] + 1'을 사용할 때만 블랙 박스를 볼 수있다. 어떤 패키지 버전을 사용하고 있습니까? –

+0

죄송합니다. 실행중인 전체 스크립트를 게시 하시겠습니까? 그렇다면 다른 점이 전혀 없음을 확신 할 수 있습니까? 또한 Python 3에서 실행 중입니까? –