如何在热图中获取数据?

时间:2015-08-19 12:46:44

标签: python numpy multidimensional-array matplotlib

我已经用Python编写了Conways Game of Life,现在我试图在热图中显示它作为输出的简单数据。
这是我目前的代码:

from Tkinter import *

import matplotlib.pyplot as plt
import time
import numpy as np
import random

size_x = 100
size_y = 10

# create the matrices
cell = [[0 for row in range(0, size_y)] for col in range(0, size_x)]
live = [[0 for row in range(0, size_y)] for col in range(0, size_x)]
temp = [[0 for row in range(0, size_y)] for col in range(0, size_x)]

# process and draw the next frame
def frame():
    process()
    draw()
    root.after(100, frame)

# load the initial data
def load(initial=0.5):
    for y in range(0, size_y):
        for x in range(0, size_x):
            if random.random()<initial: live[x][y] = 1
            temp[x][y] = 0


# Applying rules
def process():
    for y in range(0, size_y):
        for 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

    for y in range(0, size_y):
        for 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 all cells
def draw():
    nLiving = 0
    nDead = 0
    for y in range(size_y):
        for 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
    for y in range(size_y):
        for 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)
    for gen in range(1, 101):
        print str(gen) + ":",
        count()
        process()

def many_games():
    numbers = range(1,51)
    for initial in numbers:
        print initial/100.0
        one_game(initial/100.0)


many_games()
#one_game(0.5)

使用给定输入制作普通热图的代码为:

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

如何在数组中获取数据(在本例中为几代,初始化 one_game()函数的值, nLiving ) ?

1 个答案:

答案 0 :(得分:0)

我不是100%确定这是你想要的,但它产生了一个漂亮的输出热图:)

def count():
    nLiving = 0
    nDead = 0
    for y in range(size_y):
        for 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 over ten is: ", z,)
    print("%")
    return nLiving


def one_game(initial):
    load(initial)
    gen_array = []
    for 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)
    for 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()  

这只是从count函数修改到文件末尾的代码。基本上你只需要将你正在调用的函数的输出返回到正确的数据结构中,我认为......