2017-12-08 12 views
0

最初に、私がthisや他の多くの質問のようなランダムウォークラインを生成しようとしていないことを明らかにする。 thisのように、ポイントが更新されると色が変化するランダムウォークヒートマップを作成しようとしています。アニメーション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で動作していますか? –

関連する問題