Python:更新情节而不是创建新情节
Python: Update plot instead of creating a new one
我正在尝试使用 Python 对伊辛模型进行编码。
我想,我已经正确编码了,但我对动画或绘图有问题。我似乎为每个配置绘制了一个新图像,而不是更新现有的图像,导致我不需要很多已保存的图像。如果可能的话,我只想要一个正在更新的情节。
我知道,我在循环内绘图,但我不记得这是一个问题,当我想绘制每个迭代时。会不会是Seaborn的热图有问题?
我附上了我的代码:
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
import seaborn as sns
#Constants
J = 1
h = 1
kbT = 1
beta = 1
#Grid
L = 20 #Dimensions
N = L**2 #Total number of grid points
#Initial configuration
spins = 2*np.random.randint(2, size = (L,L))-1
E = []
i = 0
plt.figure()
while i < 100000:
for i in range(1,N):
i += 1
s = tuple(npr.randint(0, L, 2)) # Random initial coordinate
# x and y coordinate
(sx, sy) = s
# Periodic boundary condition
sl = (sx-1, sy)
sr = ((sx+1)%L, sy)
sb = (sx, sy-1)
st = (sx, (sy+1)%L)
# Energy
E = spins[s] * ( spins[sl] + spins[sr] + spins[sb] + spins[st] )
if E <= 0 : # If negative, flip
spins[s] *= -1
else:
x = np.exp(-E/kbT) # If positve, check condition
q = npr.rand()
if x > q:
spins[s] *= -1
# Plot (heatmap)
sns.heatmap(spins, cmap = 'magma')
plt.pause(10e-10)
plt.draw()
plt.show()
我认为函数 ion
和 clf
可以解决问题。
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
import seaborn as sns
#Constants
J = 1
h = 1
kbT = 1
beta = 1
#Grid
L = 20 #Dimensions
N = L**2 #Total number of grid points
#Initial configuration
spins = 2*np.random.randint(2, size = (L,L))-1
E = []
i = 0
plt.ion()
plt.figure()
plt.show()
while i < 100000:
for i in range(1,N):
i += 1
s = tuple(npr.randint(0, L, 2)) # Random initial coordinate
# x and y coordinate
(sx, sy) = s
# Periodic boundary condition
sl = (sx-1, sy)
sr = ((sx+1)%L, sy)
sb = (sx, sy-1)
st = (sx, (sy+1)%L)
# Energy
E = spins[s] * ( spins[sl] + spins[sr] + spins[sb] + spins[st] )
if E <= 0 : # If negative, flip
spins[s] *= -1
else:
x = np.exp(-E/kbT) # If positve, check condition
q = npr.rand()
if x > q:
spins[s] *= -1
# Plot (heatmap)
plt.clf()
sns.heatmap(spins, cmap = 'magma')
plt.pause(10e-10)
使用函数 ion
,您正在制作交互式情节,因此您需要:
- 使其具有交互性
- 显示剧情
- 清除循环中的剧情
Here ion
函数的参考。
clf
的引用是 here
我正在尝试使用 Python 对伊辛模型进行编码。
我想,我已经正确编码了,但我对动画或绘图有问题。我似乎为每个配置绘制了一个新图像,而不是更新现有的图像,导致我不需要很多已保存的图像。如果可能的话,我只想要一个正在更新的情节。
我知道,我在循环内绘图,但我不记得这是一个问题,当我想绘制每个迭代时。会不会是Seaborn的热图有问题?
我附上了我的代码:
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
import seaborn as sns
#Constants
J = 1
h = 1
kbT = 1
beta = 1
#Grid
L = 20 #Dimensions
N = L**2 #Total number of grid points
#Initial configuration
spins = 2*np.random.randint(2, size = (L,L))-1
E = []
i = 0
plt.figure()
while i < 100000:
for i in range(1,N):
i += 1
s = tuple(npr.randint(0, L, 2)) # Random initial coordinate
# x and y coordinate
(sx, sy) = s
# Periodic boundary condition
sl = (sx-1, sy)
sr = ((sx+1)%L, sy)
sb = (sx, sy-1)
st = (sx, (sy+1)%L)
# Energy
E = spins[s] * ( spins[sl] + spins[sr] + spins[sb] + spins[st] )
if E <= 0 : # If negative, flip
spins[s] *= -1
else:
x = np.exp(-E/kbT) # If positve, check condition
q = npr.rand()
if x > q:
spins[s] *= -1
# Plot (heatmap)
sns.heatmap(spins, cmap = 'magma')
plt.pause(10e-10)
plt.draw()
plt.show()
我认为函数 ion
和 clf
可以解决问题。
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
import seaborn as sns
#Constants
J = 1
h = 1
kbT = 1
beta = 1
#Grid
L = 20 #Dimensions
N = L**2 #Total number of grid points
#Initial configuration
spins = 2*np.random.randint(2, size = (L,L))-1
E = []
i = 0
plt.ion()
plt.figure()
plt.show()
while i < 100000:
for i in range(1,N):
i += 1
s = tuple(npr.randint(0, L, 2)) # Random initial coordinate
# x and y coordinate
(sx, sy) = s
# Periodic boundary condition
sl = (sx-1, sy)
sr = ((sx+1)%L, sy)
sb = (sx, sy-1)
st = (sx, (sy+1)%L)
# Energy
E = spins[s] * ( spins[sl] + spins[sr] + spins[sb] + spins[st] )
if E <= 0 : # If negative, flip
spins[s] *= -1
else:
x = np.exp(-E/kbT) # If positve, check condition
q = npr.rand()
if x > q:
spins[s] *= -1
# Plot (heatmap)
plt.clf()
sns.heatmap(spins, cmap = 'magma')
plt.pause(10e-10)
使用函数 ion
,您正在制作交互式情节,因此您需要:
- 使其具有交互性
- 显示剧情
- 清除循环中的剧情
Here ion
函数的参考。
clf
的引用是 here