我的动画图像没有使用matplotlib imshow动画更改

时间:2019-02-06 16:06:50

标签: python matplotlib animation imshow

我想为代码中的每个蒙特卡洛循环做动画。但是,该映像不会更新,并且与开始时的映像相同。

ground_state是一个2D数组,在每次调用模拟后都会更改。我已经检查过它确实在更新中被更改了

fig = pyplot.figure()
im = pyplot.imshow(ground_state, animated = True)

def update_fig(*args):
    global ground_state
    simulate(ground_state, 100000, 2.4)
    im.set_data(ground_state)
    return im

ani = animation.FuncAnimation(fig, update_fig, interval = 50)

pyplot.show()

这是模拟功能:-

def simulate(state, n, T):
    """
    Simulates at temperature T for N iterations.
    """
    def acceptance_ratio(del_E):
        """
        Calculates the acceptance ratio.
        """
        # value of beta = 1 / (boltzmann constant * T)
        beta = 1 / T
        if (del_E) < 0:
            return 1
        else:
            return e ** (-beta * (del_E))

    step = 0
    cycle = 0
    # Single flipping
    for dummy in range(n):
        switch = (random.choice(range(len(state))), 
random.choice(range(len(state))))
        neighbours = nearest_neighbours(switch[0], switch[1], len(state))
        change_E = 0
        for pos in neighbours:
            change_E += 2 * J * state[pos[0]][pos[1]] * state[switch[0][switch[1]]
    p = acceptance_ratio(change_E)
    step += 1
    if step == 100 * 100:
        cycle += 1
    rand_num = random.random()
    if rand_num <= p:
        state[switch[0]][switch[1]] *= -1
    return state

1 个答案:

答案 0 :(得分:0)

我们不知道"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\gacutil.exe" /i Microsoft.SqlServer.BatchParser.dll 函数内部会发生什么,但这似乎是问题的根源,因为否则,代码将运行良好并具有动画效果,

simulate

animated image