在Matplotlib中使用动画的Colormap问题 [英] Colormap issue using animation in matplotlib

查看:171
本文介绍了在Matplotlib中使用动画的Colormap问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 matplotlib.animation 为名为 arr 的3D数组中的数据设置动画。我使用 h5py 库从h5文件中读取数据,一切正常。但是当使用动画时,颜色图卡在数据范围的第一帧中,经过一些步骤后,它在绘制时会显示未归一化的颜色。

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.

这是我的代码:

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


推荐答案

我认为您想将 set_clim()替换为

p.autoscale()

不带参数的 set_clim()是不带参数的

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

也就是说,在动画中间更改颜色比例似乎很容易引起误解。

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

您还应该使用 set_data 代替 set_array (根据文档)。

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

这篇关于在Matplotlib中使用动画的Colormap问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆