matplotlib - Python modules to plot game of life script -


i wanted know modules recommend plotting lists of coordinates on x-y plane are. purpose implement game of life; i've tried matplotlib.pyplot i've got issues axes ratio , in general doesn't seem best way plot sort of animation. main idea in script plot world in cells live, tick given time.sleep.

import matplotlib.pyplot plt  class cell:     #cell class, coordinates , on-off state     def __init__(self,x=0,y=0,state=0):         self.state=state         self.x=x         self.y=y         self.coord=[self.x,self.y]     def __repr__(self):         if self.state:                   return "*"         else:             return " "     #methods turn on , off cells:     def turn_on(self):         self.state=1         return self     def turn_off(self):         self.state=0         return self  class mondo:     #the world in cells live:     def __init__(self,width=10,height=30,starting_cells=[]):         self.width=width         self.height=height         self.grid=[[cell(i,j) j in range(height)] in range(width)]         cell in starting_cells:             self.grid[cell.x][cell.y]=cell      def __repr__(self):     #this method needed if wanted use print statement instead of matplotlib         return "_"*(2*self.height+3)+"\n"+"\n".join([" ".join(["|"]+[" "+str(cell)+" " cell in self.grid[row]]+["|"]) row in range(self.width)])+"\n"+"_"*(2*self.height+3)     def plotlib(self):         #this best can do:         xaxis=[cell.x in range(self.width) cell in self.grid[i] if    cell.state]         yaxis=[cell.y in range(self.width) cell in self.grid[i] if cell.state]         plt.axis([0,self.width, 0, self.height])         return plt.show(plt.scatter(xaxis,yaxis,c="000000", marker='s'))      def __getitem__(self,row):         return self.grid[row]      def pos_cell(self,coord):         #retrieves cell given coordinates         return self.grid[coord[0]][coord[1]]     def cell_state(self,coord):         #outputs cell's state on given coord         return self.pos_cell(coord).state     def neighbors_state(self,coord,cont=0):         #sums neghbors' cell state         cell=self.pos_cell(coord)         x,y=cell.x,cell.y         hor in [-1,0,1]:             ver in [-1,0,1]:                 if not hor==ver==0 , (0<=x+hor<self.width , 0<=y+ver<self.height):                     cont+=self.cell_state([(x+hor)%self.width,(y+ver)%self.height])         return cont      def alive_dead(self,coord):         #for each cell, tells wether must switch state or not         cell=self.pos_cell(coord)         if not cell.state:             if self.neighbors_state(cell.coord)==3:                 return true             else:                 return false         elif cell.state:             if self.neighbors_state(cell.coord) in [2,3]:                 return true             else:                 return false #nb starting cells must given list, not grid def game_of_life(width,height,starting_cells):     #the heart of script     test=[[cell(i,j) j in range(height)] in range(width)]     world=mondo(width,height,starting_cells)     world.plotlib()     in range(world.width):         j in range(world.height):             if world.alive_dead([i,j]):                 test[i][j].turn_on()      starting_cells=[test[i][j] in range(width) j in range(height)]     plt.pause(0.5)     return game_of_life(width,height,starting_cells)   #example of oscillator: #print game_of_life(10,10,[cell(1,2,1),cell(2,3,1),cell(3,1,1),cell(3,2,1),cell(3,3,1)])  #example of gosper gun: gosper=[[cell(i,j) j in range(70)] in range(30)] in range(len(gosper)):     j in range(len(gosper[0])):         if [i,j] in [[1,26],[2,24],[2,26],[3,14],[3,15],[3,22],[3,23],[3,36],[3,37],[4,36],[4,37],[4,22],[4,23],[4,13],[4,17],[5,12],[5,18],[5,22],[5,23],[5,1],[5,2],[6,1],[6,2],[6,12],[6,16],[6,18],[6,19],[6,24],[6,26],[7,26],[7,18],[7,12],[8,13],[8,17],[9,14],[9,15]]:             gosper[i][j].turn_on() gosp=[gosper[i][j] in range(len(gosper)) j in range(len(gosper[0])) if gosper[i][j].state] print game_of_life(30,70,gosp)  #mondo(30,70,gosp).plotlib() #print mondo(30,70,gosp) 

i gave @ matplotlib.animation maybe there's stuff me use uni assignment.

is there module can use print simple animation? should try harder matplotlib.pyplot, , if so, how can adjust axes ratio , stuff? surfing questions found module graphics, may useful?

thank you


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -