2013-04-14 64 views
0

我試圖通過數值求解熱方程,並使用顏色映射實時顯示解決方案。不管怎樣,地圖中的顏色都沒有更新。是什麼原因?這是代碼:matplotlib imshow未更新

from __future__ import division 
from pylab import * 

dx, dy = 0.01, 0.01 
D = 0.01 
dx2, dy2 = dx**2 ,dy**2 
dt = (dx2*dy2)/(2*D*(dx2+dy2)) 
endt = 0.1 
Nt = int (endt/dt) 
endX,endY = 1,1 
nx, ny = int(endX/dx), int(endY/dy) 
T = zeros([nx,ny]) 
Tcopy = zeros([nx,ny]) 
"""initial conditions""" 

for i in range(nx): 
for j in range(ny): 
    if(((i*dx - 0.5)**2 +(j*dy - 0.5)**2 < 0.1) and ((i*dx - 0.5)**2 +(j*dy - 0.5)**2 >0.05)): 
    T[i][j] = 10 


def integrate(T,Tcopy): 

T[1:-1, 1:-1] = Tcopy[1:-1, 1:-1] + D*dt*((Tcopy[2:, 1:-1] - 2*Tcopy[1:-1, 1:-1] + T[:-2, 1:-1])/dx2 + (Tcopy[1:-1, 2:] - 2*Tcopy[1:-1, 1:-1] + T[1:-1, :-2])/dy2 ) 
Tcopy = copy(T) 
return Tcopy,T 

x = arange(0, endX, dx) 
y = arange(0, endY, dy) 
X,Y = meshgrid(x, y) 

"""creating the plot""" 

fig, ax_lst = plt.subplots(1, 1) 
im = ax_lst.imshow(T, interpolation='nearest', 
          origin='bottom', 
          aspect='auto', 
          vmin=np.min(T), 
          vmax=np.max(T), 
          cmap='hot',extent=[0,1,0,1]) 
fig.colorbar(im) 

"""main loop""" 

for t in range(Nt): 
im.set_data(T) 
plt.draw() 
plt.pause(0.1) 
Tcopy,T = integrate(T,Tcopy) 
+1

你是什麼意思「地圖中的顏色」?你想改變彩條的限制嗎? – tacaswell

+1

這段代碼似乎對我來說工作得很好,我看到隨着時間的推移,這個環傳播並且很酷。 – tacaswell

+0

在任何齒我想看到不同地區的溫度按顏色(較冷區域,較暗的顏色)。現在,不僅顏色條的限制不會改變(我不希望它改變),而且顏色在模擬過程中根本不會改變。如果某個點的T值從100變爲10,我預計該區域的顏色會發生變化,但不會發生。從開始到結束都是一樣的紅色。但是,如果我創建了2個不同的情節,沒有動畫,一個在開始和一個在結尾,可以注意到兩者之間的顏色差異。 – user1767774

回答

1

比較左,右:

from __future__ import division 
from pylab import * 

dx, dy = 0.01, 0.01 
D = 0.01 
dx2, dy2 = dx**2 ,dy**2 
dt = (dx2*dy2)/(2*D*(dx2+dy2)) 
endt = 0.1 
Nt = int (endt/dt) 
endX,endY = 1,1 
nx, ny = int(endX/dx), int(endY/dy) 
T = zeros([nx,ny]) 
Tcopy = zeros([nx,ny]) 
"""initial conditions""" 

for i in range(nx): 
for j in range(ny): 
    if(((i*dx - 0.5)**2 +(j*dy - 0.5)**2 < 0.1) and ((i*dx - 0.5)**2 +(j*dy - 0.5)**2 >0.05)): 
    T[i][j] = 10 


def integrate(T,Tcopy): 

T[1:-1, 1:-1] = Tcopy[1:-1, 1:-1] + D*dt*((Tcopy[2:, 1:-1] - 2*Tcopy[1:-1, 1:-1] + T[:-2, 1:-1])/dx2 + (Tcopy[1:-1, 2:] - 2*Tcopy[1:-1, 1:-1] + T[1:-1, :-2])/dy2 ) 
Tcopy = copy(T) 
return Tcopy,T 

x = arange(0, endX, dx) 
y = arange(0, endY, dy) 
X,Y = meshgrid(x, y) 

"""creating the plot""" 

fig, ax_lst = plt.subplots(1, 2) 
ax_lst[0].imshow(T, interpolation='nearest', 
          origin='bottom', 
          aspect='auto', 
          vmin=np.min(T), 
          vmax=np.max(T), 
          cmap='hot',extent=[0,1,0,1]) 
im = ax_lst[1].imshow(T, interpolation='nearest', 
          origin='bottom', 
          aspect='auto', 
          vmin=np.min(T), 
          vmax=np.max(T), 
          cmap='hot',extent=[0,1,0,1]) 
fig.colorbar(im) 

"""main loop""" 

for t in range(Nt): 
im.set_data(T) 
plt.draw() 
plt.pause(0.1) 
Tcopy,T = integrate(T,Tcopy) 
print np.mean(T), np.max(T), np.min(T) 

顏色正在改變(至少在我的機器上),只是它很微妙,很難看清。

你可能想嘗試數比例以及

im = ax_lst.imshow(T, interpolation='nearest', 
         origin='bottom', 
         aspect='auto', 
         vmin=np.min(T) + .01, # so 0 doesn't blow up the log 
         vmax=np.max(T), 
         cmap='hot',extent=[0,1,0,1], 
         norm=matplotlib.colors.LogNorm(clip=True),) 
+0

非常感謝! ( - : – user1767774

+0

@ user1767774你是否充分理清了這一點? – tacaswell

1

如果你想要的顏色條代表改變動畫的每一幀的範圍,然後修改您的for循環如下:

for t in range(Nt): 
im.set_data(T) 
im.set_clim(T.min(),T.max()) 
plt.draw() 
plt.pause(0.1) 
Tcopy,T = integrate(T,Tcopy) 
+0

謝謝!我如何更新地圖中的顏色,而不更新顏色條?我想在整個模擬過程中保持相同的顏色範圍。 – user1767774

+1

你必須創建你自己的色彩地圖。查看pydoc中的matplotlib.colors.LinearSegmentedColormap並在StackOverflow上參考[http://stackoverflow.com/questions/9893440/python-matplotlib-colormap](here)。您可以使用上面的AxesImage實例的set_cmap方法來指定顏色映射。 – mtadd