Python 为什么matplotlib.animation在大窗口时比在小窗口时慢?
所以,我写了一个兰顿的蚂蚁程序Python 为什么matplotlib.animation在大窗口时比在小窗口时慢?,python,numpy,matplotlib,animation,Python,Numpy,Matplotlib,Animation,所以,我写了一个兰顿的蚂蚁程序 #!/bin/env python import matplotlib.animation as ani import matplotlib.pyplot as plt import numpy as np w,h = 100,100 d = np.array([0,1]) p = np.array([w//2,h//2]) grid = np.ones((w,h),dtype=int) #MAIN fig
#!/bin/env python
import matplotlib.animation as ani
import matplotlib.pyplot as plt
import numpy as np
w,h = 100,100
d = np.array([0,1])
p = np.array([w//2,h//2])
grid = np.ones((w,h),dtype=int)
#MAIN
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(1,1,1)
img = ax.imshow(grid,vmin=-1,vmax=1)
tle = ax.text(0.5,0.95,"",bbox={'facecolor':'w','alpha':0.5,'pad':5},transform=ax.transAxes,ha="center")
ax.tick_params(axis='x',which='both',bottom=False,top=False,labelbottom=False,labeltop=False)
ax.tick_params(axis='y',which='both',right=False,left=False,labelright=False,labelleft=False)
def langtons_ant_animator(frame):
global w,h,d,p,grid
v = grid[p[0],p[1]]
grid[p[0],p[1]] = -v
d = [[0,v],[-v,0]] @ d
p = (p+d) % (w,h)
img.set_data(grid)
tle.set_text(f'{frame: 9} Steps; Point ({p[0]: 3},{p[1]: 3}), Direction ({d[0]: 2},{d[1]: 2})')
return [tle,img]
animation = ani.FuncAnimation(
fig = fig,
func = langtons_ant_animator,
interval = 1,
blit = True,
)
plt.show(block=True)
当这个程序运行时,我可以动态地改变窗口大小。如果我把蚂蚁的尺寸弄得很小(1英寸乘1英寸),蚂蚁就会以很快的速度移动,接近每秒1000步。但是如果我把窗口保持在(10英寸乘10英寸),它会以每秒100步左右的速度更新。你可能会认为更新的像素数会影响速度,但事实并非如此;将窗口更改为10001000,动画以10英寸乘10英寸的速度运行