2

pcolor を使用して 2D 配列から情報をプロットします。ただし、配列内の情報は反復中に変更されるため、変更をリアルタイムで視覚化するために、カラーマップを動的に更新したいと考えています。最も簡単な方法でそれを行うにはどうすればよいですか?

編集 - 例:

from __future__ import division
from pylab import *
import random

n = 50 # number of iterations
x = arange(0, 10, 0.1)
y = arange(0, 10, 0.1)
T = zeros([100,100]) # 10/0.1 = 100
X,Y = meshgrid(x, y)

"""initial conditions"""
for x in range(100):
 for y in range(100):
  T[x][y] = random.random()

pcolor(X, Y, T, cmap=cm.hot, vmax=abs(T).max(), vmin=0)
colorbar()
axis([0,10,0,10])
show() # colormap of the initial array

"""main loop"""

for i in range(n):
 for x in range(100):
  for y in range(100):
   T[x][y] += 0.1 # here i do some calculations, the details are not important

 # here I want to update the color map with the new array (T)

ありがとう

4

3 に答える 3

3

imshowdoc )を使用することをお勧めします:

# figure set up
fig, ax_lst = plt.subplots(2, 1)
ax_lst = ax_lst.ravel()

#fake data
data = rand(512, 512)
x = np.linspace(0, 5, 512)
X, Y = meshgrid(x, x)

data2 = np.sin(X ** 2 + Y **2)
# plot the first time#fake data

im = ax_lst[0].imshow(data, interpolation='nearest', 
                            origin='bottom', 
                            aspect='auto', # get rid of this to have equal aspect
                            vmin=np.min(data),
                            vmax=np.max(data), 
                            cmap='jet')

cb = plt.colorbar(im)

pc = ax_lst[1].pcolor(data)
cb2 = plt.colorbar(pc)

imshow でデータを更新するには、データ配列を設定するだけで、すべての正規化とカラー マッピングが処理されます。

# update_data (imshow)
im.set_data(data2) 
plt.draw()

同じことをpcolor行うには、自分で正規化とカラー マッピングを行う必要があります (そして、行優先と列優先を正しく推測します):

my_cmap = plt.get_cmap('jet')
#my_nom = # you will need to scale your read data between [0, 1]
new_color = my_cmap(data2.T.ravel())
pc.update({'facecolors':new_color})

draw() 
于 2013-04-13T20:46:53.790 に答える