简体   繁体   中英

How to animate matplotlib.imshow()?

I am a physics student trying to simulate the 2D Ising model using the Metropolis algorithm in Python. I wanted to see the time evolution of imshow() and used the following code below. But the program outputs a blank graph. What do I do? Thank you for your help!!

n = 50
ims =[]

def lattice(p):
    init_random = np.random.random((n,n))
    d = np.zeros((n,n))
    d[init_random>=p] = 1
    d[init_random<p] = -1
    return d

def energy(lattice):
    kern = generate_binary_structure(2, 1) 
    kern[1][1] = False
    e = -lattice * convolve(lattice, kern, mode='constant', cval=0)
    return e.sum()

def metropolis(s, b, t, e):
    s = s.copy()
    for t in range(0,t-1):
        x = np.random.randint(0,n)
        y = np.random.randint(0,n)
        si = s[x,y]
        sf = -1*si
        
        ei = 0
        ef = 0
        
        if x > 0:
            ei += -si*s[x-1,y]
            ef += -sf*s[x-1,y]
        if x < n-1:
            ei += -si*s[x+1,y]
            ef += -sf*s[x+1,y]
        if y > 0:
            ei += -si*s[x,y-1]
            ef += -sf*s[x,y-1]
        if y < n-1:
            ei += -si*s[x,y+1]
            ef += -sf*s[x,y+1]
            
        de = ef - ei
        
        if (de>0)*(np.random.random() < np.exp(-b*de)):
            s[x,y] = sf
        elif (de<=0):
            s[x,y] = sf
        
        im = plt.imshow(s, animated = True)
        ims.append([im])
        
    return ims

u = lattice(0.25)
e = energy(u)
fig = plt.figure(figsize=(6,6))
ims = metropolis(u, 0.7, 100, e)

ani = animation.ArtistAnimation(fig, ims, interval = 50, blit = True, repeat_delay = 100)
plt.show()

I only needed to add the imports and change the def energy section (specifically the e= line). It works nicely in my system.

import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy import ndimage, convolve

n = 50
ims =[]

def lattice(p):
    init_random = np.random.random((n,n))
    d = np.zeros((n,n))
    d[init_random>=p] = 1
    d[init_random<p] = -1
    return d

def energy(lattice):
    kern = ndimage.generate_binary_structure(2, 1) 
    kern[1][1] = False
    e = -lattice * ndimage.convolve(lattice, kern, mode='constant', cval=1)
    return e.sum()

def metropolis(s, b, t, e):
    s = s.copy()
    for t in range(0,t-1):
        x = np.random.randint(0,n)
        y = np.random.randint(0,n)
        si = s[x,y]
        sf = -1*si
        
        ei = 0
        ef = 0
        
        if x > 0:
            ei += -si*s[x-1,y]
            ef += -sf*s[x-1,y]
        if x < n-1:
            ei += -si*s[x+1,y]
            ef += -sf*s[x+1,y]
        if y > 0:
            ei += -si*s[x,y-1]
            ef += -sf*s[x,y-1]
        if y < n-1:
            ei += -si*s[x,y+1]
            ef += -sf*s[x,y+1]
            
        de = ef - ei
        
        if (de>0)*(np.random.random() < np.exp(-b*de)):
            s[x,y] = sf
        elif (de<=0):
            s[x,y] = sf
        
        im = plt.imshow(s, animated = True)
        ims.append([im])
        
    return ims

u = lattice(0.25)
e = energy(u)
fig = plt.figure(figsize=(6,6))
ims = metropolis(u, 0.7, 100, e)

ani = animation.ArtistAnimation(fig, ims, interval = 50, blit = True, repeat_delay = 100)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM