[英]How to update an existing record instead of creating a new one in django?
[英]Python: Update plot instead of creating a new one
我正在嘗試使用 Python 對 Ising 模型進行編碼。
我想,我已經正確編碼了,但是我的動畫或繪圖有問題。 我似乎為每個配置繪制了一張新圖像,而不是更新現有的圖像,從而導致我不需要的很多已保存的圖像。 如果可能的話,我只想要一個正在更新的圖。
我知道,我在循環內繪圖,但我不記得這是一個問題,當我想繪制每次迭代時。 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
函數使繪圖具有交互性,因此您需要:
這里是ion
功能的參考。
clf
參考在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.