Python:更新绘图,而不是创建新绘图
我正在尝试使用Python编写Ising模型 我想,我已经正确编码了,但是我在动画或绘图方面有问题。我似乎为每个配置绘制了一个新的映像,而不是更新现有的映像,从而生成了许多我不需要的已保存映像。我只想要一个正在更新的绘图,如果可能的话 我知道,我在循环中绘制,但我不记得这是一个问题,当我想绘制每个迭代时。Seaborn的热图会有问题吗 我已附上我的代码:Python:更新绘图,而不是创建新绘图,python,matplotlib,seaborn,scientific-computing,Python,Matplotlib,Seaborn,Scientific Computing,我正在尝试使用Python编写Ising模型 我想,我已经正确编码了,但是我在动画或绘图方面有问题。我似乎为每个配置绘制了一个新的映像,而不是更新现有的映像,从而生成了许多我不需要的已保存映像。我只想要一个正在更新的绘图,如果可能的话 我知道,我在循环中绘制,但我不记得这是一个问题,当我想绘制每个迭代时。Seaborn的热图会有问题吗 我已附上我的代码: import numpy as np import numpy.random as npr import matplotlib.pyplot
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()
将numpy导入为np
将numpy.random作为npr导入
将matplotlib.pyplot作为plt导入
导入seaborn作为sns
#常数
J=1
h=1
kbT=1
β=1
#网格
L=20#尺寸
N=L**2#网格点总数
#初始配置
自旋=2*np.random.randint(2,size=(L,L))-1
E=[]
i=0
plt.图()
当我<100000时:
对于范围(1,N)内的i:
i+=1
s=元组(npr.randint(0,L,2))#随机初始坐标
#x和y坐标
(sx,sy)=s
#周期边界条件
sl=(sx-1,sy)
sr=((sx+1)%L,sy)
sb=(sx,sy-1)
st=(sx,(sy+1)%L)
#精力
E=自旋[s]*(自旋[sl]+自旋[sr]+自旋[sb]+自旋[st])
如果E q:
自旋[s]*=-1
#绘图(热图)
sns.heatmap(自旋,cmap='magma')
plt.暂停(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)
将numpy导入为np
将numpy.random作为npr导入
将matplotlib.pyplot作为plt导入
导入seaborn作为sns
#常数
J=1
h=1
kbT=1
β=1
#网格
L=20#尺寸
N=L**2#网格点总数
#初始配置
自旋=2*np.random.randint(2,size=(L,L))-1
E=[]
i=0
plt.ion()
plt.图()
plt.show()
当我<100000时:
对于范围(1,N)内的i:
i+=1
s=元组(npr.randint(0,L,2))#随机初始坐标
#x和y坐标
(sx,sy)=s
#周期边界条件
sl=(sx-1,sy)
sr=((sx+1)%L,sy)
sb=(sx,sy-1)
st=(sx,(sy+1)%L)
#精力
E=自旋[s]*(自旋[sl]+自旋[sr]+自旋[sb]+自旋[st])
如果E q:
自旋[s]*=-1
#绘图(热图)
plt.clf()
sns.heatmap(自旋,cmap='magma')
plt.暂停(10e-10)
使用函数ion
可以使绘图具有交互性,因此需要:
- 让它互动
- 展示情节
- 清除循环中的绘图
ion
功能的参考
clf
的参考是我认为您正在寻找函数ion
,您可以将其用作plt.ion()
,看看它是否成功!非常感谢。现在我只得到一个“”,而不是很多。