如何使用 matplotlib 对一组点进行动画处理?

Yul*_*ian 3 python animation matplotlib conways-game-of-life

我已经实现了康威的生命游戏:

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
Run Code Online (Sandbox Code Playgroud)

我想可视化结果,所以我尝试修改Matplotlib 动画示例中给定的示例:

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()
Run Code Online (Sandbox Code Playgroud)

但这只是绘制了初始点

Imp*_*est 5

您拥有的代码实际上应该会产生错误。问题是您glider在分配之前引用它。

注意 python 函数中变量的局部范围。例如尝试

a = 0
def f():
    a = a + 1
f()
Run Code Online (Sandbox Code Playgroud)

这会给你同样的错误。

在康威生命游戏的代码中,您可以通过glider向全局范围提供global glider. 还要确保您的轴限制允许看到动画。

完整示例:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述