如何使用 matplotlib 为一组点设置动画?
How can I animate a set of points with matplotlib?
我有一个已实施的康威生命游戏:
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
我想可视化结果,所以我尝试修改给定的示例 Matplotlib animation example:
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()
但这只是情节the initial points。
您的代码实际上应该会产生错误。问题是您在分配之前引用 glider
。
注意 python 函数中变量的局部作用域。例如。尝试
a = 0
def f():
a = a + 1
f()
这会给你同样的错误。
在您的康威生命游戏代码中,您可以通过使 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()
我有一个已实施的康威生命游戏:
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
我想可视化结果,所以我尝试修改给定的示例 Matplotlib animation example:
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()
但这只是情节the initial points。
您的代码实际上应该会产生错误。问题是您在分配之前引用 glider
。
注意 python 函数中变量的局部作用域。例如。尝试
a = 0
def f():
a = a + 1
f()
这会给你同样的错误。
在您的康威生命游戏代码中,您可以通过使 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()