2017-10-20 1 views
2

Ich habe ein implementiertes Conways Spiel des Lebens wie:Wie kann ich eine Menge Punkte mit Matplotlib animieren?

def neighbors(point): 
    x, y = point 
    for i, j in itertools.product(range(-1, 2), repeat=2): 
     if any((i, j)): 
      yield (x + i, y + j) 

def advance(board): 
    newstate = set() 
    recalc = board | set(itertools.chain(*map(neighbors, board))) 

    for point in recalc: 
     count = sum((neigh in board) 
       for neigh in neighbors(point)) 
     if count == 3 or (count == 2 and point in board): 
      newstate.add(point) 

    return newstate 

ich das Ergebnis visualisieren möchten, also versuchte ich das gegebene Beispiel von Matplotlib animation example zu ändern:

glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)]) 

fig, ax = plt.subplots() 

x, y = zip(*glider) 
mat, = ax.plot(x, y, 'o') 

def animate(i): 
    glider = advance(glider) 
    x, y = zip(*glider) 
    mat.set_data(x, y) 
    return mat, 

ani = animation.FuncAnimation(fig, animate, interval=50) 
plt.show() 

aber das Plots nur the initial points.

+0

Sie könnten in anderen matplotlib Implementierungen des Spiels des Lebens interessiert sein, wie [dieser] (https://stackoverflow.com/questions/45653550/ stop-animation-conways-game-of-life) oder [diese] (https://stackoverflow.com/questions/46196346/why-does-my-game-of-life-simulation-slow-down-to-to- a-Crawl-in-Sekunden-Matplot). – ImportanceOfBeingErnest

Antwort

3

Der Code, den Sie haben, sollte tatsächlich einen Fehler erzeugen. Das Problem ist, dass Sie auf glider verweisen, bevor Sie es zuweisen.

Beachten Sie den lokalen Gültigkeitsbereich von Variablen in Python-Funktionen. Z.B. versuchen Sie

a = 0 
def f(): 
    a = a + 1 
f() 

, die Ihnen den gleichen Fehler geben wird.

In Ihrem Code von Conways Spiel des Lebens können Sie dies umgehen, indem Sie glider für den globalen Gültigkeitsbereich verfügbar machen, global glider. Stellen Sie außerdem sicher, dass Ihre Achsenlimits die Anzeige der Animation ermöglichen.

Komplettes Beispiel:

import itertools 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

def neighbors(point): 
    x, y = point 
    for i, j in itertools.product(range(-1, 2), repeat=2): 
     if any((i, j)): 
      yield (x + i, y + j) 

def advance(board): 
    newstate = set() 
    recalc = board | set(itertools.chain(*map(neighbors, board))) 

    for point in recalc: 
     count = sum((neigh in board) 
       for neigh in neighbors(point)) 
     if count == 3 or (count == 2 and point in board): 
      newstate.add(point) 

    return newstate 

glider = set([(0, 0), (1, 0), (2, 0), (0, 1), (1, 2)]) 

fig, ax = plt.subplots() 

x, y = zip(*glider) 
mat, = ax.plot(x, y, 'o') 

def animate(i): 
    global glider 
    glider = advance(glider) 
    x, y = zip(*glider) 
    mat.set_data(x, y) 
    return mat, 

ax.axis([-15,5,-15,5]) 
ani = animation.FuncAnimation(fig, animate, interval=50) 
plt.show() 

enter image description here

+0

Aus Neugier @ImportanceOfBeinness, wie hast du dieses Gif erstellt? –

+1

@ReblochonMasqueSie können [jede Animation speichern] (http://matplotlib.org/api/_as_gen/matplotlib.animation.Animation.html#matplotlib.animation.Animation.save) in diesem Fall: 'ani.save (" output "). gif "writer =" imagemagick ")". – ImportanceOfBeingErnest

+0

Vielen Dank! –

Verwandte Themen