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()
有帮助吗?

解决方案

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top