函数可以单独工作,但不会在动画中进行动画处理。FuncAnimation?

时间:2019-02-12 16:54:15

标签: python python-3.x

我已经建立了一个基本模型来模拟Bak-Tang论文中关于自组织临界性的沙堆,并且可以在掉落所有沙粒后得到晶格的最终图。

我想通过动画对过程进行动画处理。FuncAnimation可以观察到在掉落一定数量的沙粒期间晶格的生长情况,但是我一直得到没有动画的静态图像。

# table width
n=5
SIZE = (n, n)

# max height to trigger avalanche (dispersion of sand)
MAXH = 4

# number of sand grains to be dropped
SGD = 20000

# RLC = (n//2,n//2)
field = np.zeros((SIZE[0] + 2, SIZE[1] + 2))

s = []

def drop():
    # random co ords to drop sand grain in
    RLC = (np.random.randint(n), np.random.randint(n))
    field[RLC[0] + 1, RLC[1] + 1] += 1
    return field

# increase piles of surrounding piels from too high piles

def disperse():
    while np.max(field) >= MAXH:

    # Identify critical sites

        # find the highest piles
        toohigh = field >= MAXH

    # shift critical piles
        # decrease piles
        field[toohigh] -= MAXH
        # shift 1 sand up
        field[1:, :][toohigh[:-1, :]] += MAXH / 4
        # shift 1 sand down
        field[:-1, :][toohigh[1:, :]] += MAXH / 4
        # shift 1 sand left
        field[:, 1:][toohigh[:, :-1]] += MAXH / 4
        # shift 1 sand right
        field[:, :-1][toohigh[:, 1:]] += MAXH / 4

    # reset the overspill at edges, anything that exceeds table boundary becomes 0

        # top edge
        field[0:1, :] = 0
        # bottom edge
        field[1 + SIZE[0]:, :] = 0
        # left edge
        field[:, 0:1] = 0
        # right edge
        field[:, 1 + SIZE[1]:] = 0
        # output new field
    return field

def animate(i):
    drop()
    disperse()

fig = plt.figure(figsize=(5, 5), dpi=100.0, frameon=False)
ax = plt.Axes(fig, [0, 0, 1, 1])
ax.set_axis_off()
fig.add_axes(ax)
ax = ax.imshow(field, cmap='gray')
plt.savefig('test.png', frameon=False)
cb = plt.colorbar(ax, shrink=0.9)
anim = animation.FuncAnimation(fig, animate, frames=SGD, interval=1)
plt.show()

我需要获得一个动画,显示出沙粒运动的晶格演变过程,但是我只能得到初始场为零的静态图像。

0 个答案:

没有答案