Colormap issue using animation in matplotlib

一个人想着一个人 提交于 2019-12-25 04:08:02

问题


I use matplotlib.animation to animate data in a 3D array named arr. I read data from a h5 file using h5py library and everything is OK. But when using animation, the colormap got stuck in first frame of the data range, and after some steps it shows unnormalized colors while plotting.

Here is my code:

import numpy as np
import h5py
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.cm as cm

f = h5py.File('ez.h5','r')
arr = f["ez"][:,:,:]
f.close()

fig = plt.figure()

i = 0
p = plt.imshow(arr[:,:,0], interpolation='bilinear', cmap=cm.RdYlGn)

def updatefig(*args):
    global i
    i += 1
    if (i==333):
        i = 0
    p.set_array(arr[:,:,i])
    plt.clim()
    return p,

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

回答1:


I think you want to replace set_clim() with

p.autoscale()

With no arguments, set_clim() is a no-op.

That said, changing your color scale in the middle of an animations seems very misleading.

You should also use set_data instead of set_array (according to the docs).



来源:https://stackoverflow.com/questions/16657334/colormap-issue-using-animation-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!