I plot information from a 2D array with pcolor. however, the information in the array is changed over the iterations, and I want to update the color map dynamically, in order to visualize the changes in real time. How can I do it in the most simple way?
Edit - example:
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)
Thanks
I would suggest using imshow
(doc):
# 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)
To updata the data with imshow, just set the data array, and it takes care of all of the normalization and color mapping for you:
# update_data (imshow)
im.set_data(data2)
plt.draw()
To do the same with thing with pcolor
you need to do the normalization and color mapping your self (and guess the row-major vs column major right):
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()