python - How do I get my data in my heatmap? -
i've programmed conways game of life in python , i'm trying display simple data gives me output in heat map.
current code:
from tkinter import * import matplotlib.pyplot plt import time import numpy np import random size_x = 100 size_y = 10 # create matrices cell = [[0 row in range(0, size_y)] col in range(0, size_x)] live = [[0 row in range(0, size_y)] col in range(0, size_x)] temp = [[0 row in range(0, size_y)] col in range(0, size_x)] # process , draw next frame def frame(): process() draw() root.after(100, frame) # load initial data def load(initial=0.5): y in range(0, size_y): x in range(0, size_x): if random.random()<initial: live[x][y] = 1 temp[x][y] = 0 # applying rules def process(): y in range(0, size_y): x in range(0, size_x): lives = live_neighbors(x,y) if live[x][y] == 1: if lives < 2 or lives > 3: temp[x][y] = 0 else: temp[x][y] = 1 if live[x][y] == 0: if lives == 3: temp[x][y] = 1 else: temp[x][y] = 0 y in range(0, size_y): x in range(0, size_x): live[x][y] = temp[x][y] # live = temp # count live neighbors def live_neighbors(a,b): lives = 0 if live[a][(b+1)%size_y] == 1: lives += 1 if live[a][(b-1)%size_y] == 1: lives += 1 if live[(a+1)%size_x][b] == 1: lives += 1 if live[(a+1)%size_x][(b+1)%size_y] == 1: lives += 1 if live[(a+1)%size_x][(b-1)%size_y] == 1: lives += 1 if live[(a-1)%size_x][b] == 1: lives += 1 if live[(a-1)%size_x][(b+1)%size_y] == 1: lives += 1 if live[(a-1)%size_x][(b-1)%size_y] == 1: lives += 1 return lives # draw cells def draw(): nliving = 0 ndead = 0 y in range(size_y): x in range(size_x): if live[x][y]==0: canvas.itemconfig(cell[x][y], fill="black") ndead+=1 if live[x][y]==1: canvas.itemconfig(cell[x][y], fill="white") nliving+=1 print nliving,ndead # count cells def count(): nliving = 0 ndead = 0 y in range(size_y): x in range(size_x): if live[x][y]==0: ndead+=1 if live[x][y]==1: nliving+=1 z = nliving / 10.0 print z, print "%" def one_game(initial): load(initial) gen in range(1, 101): print str(gen) + ":", count() process() def many_games(): numbers = range(1,51) initial in numbers: print initial/100.0 one_game(initial/100.0) many_games() #one_game(0.5)
the code making normal heat map given input be:
fig, ax = plt.subplots(1) x = np.array( [[11,12,13], [21,22,23], [31,32,33]] ) p = ax.pcolormesh(x) fig.colorbar(p) plt.show()
how data (which in case be, generations, value initializes one_game() function, , nliving) array?
i'm not 100% sure you're intending, produced pretty output heat map :)
def count(): nliving = 0 ndead = 0 y in range(size_y): x in range(size_x): if live[x][y]==0: ndead+=1 if live[x][y]==1: nliving+=1 z = nliving / 10.0 print("nliving on ten is: ", z,) print("%") return nliving def one_game(initial): load(initial) gen_array = [] gen in range(1, 101): print("gen: ", str(gen) + ":",) nliving = count() process() gen_array.append(nliving) return gen_array def many_games(): gen_output = [] numbers = range(1,51) initial in numbers: print(initial/100.0) gen_array = one_game(initial/100.0) gen_output.append(gen_array) return gen_output gen_output = many_games() #one_game(0.5) fig, ax = plt.subplots(1) x = np.array( gen_output ) p = ax.pcolormesh(x) fig.colorbar(p) plt.show()
that code modified count function end of file. need return output functions you're calling right kind of data structures, think...
Comments
Post a Comment