【问题标题】:How to animate the colorbar in matplotlib如何在 matplotlib 中为颜色栏设置动画
【发布时间】:2017-01-21 04:08:15
【问题描述】:

我有一个动画,其中数据的范围变化很​​大。我想要一个 colorbar 来跟踪数据的最大值和最小值(即我不希望它被修复)。问题是如何做到这一点。

理想情况下,我希望 colorbar 在自己的轴上。

我尝试了以下四件事

1。天真的方法

问题:为每一帧绘制一个新的颜色条

#!/usr/bin/env python
"""
An animated image
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111)


def f(x, y):
    return np.exp(x) + np.sin(y)

x = np.linspace(0, 1, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

frames = []

for i in range(10):
    x       += 1
    curVals  = f(x, y)
    vmax     = np.max(curVals)
    vmin     = np.min(curVals)
    levels   = np.linspace(vmin, vmax, 200, endpoint = True)
    frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
    cbar     = fig.colorbar(frame)
    frames.append(frame.collections)

ani = animation.ArtistAnimation(fig, frames, blit=False)

plt.show()

2。添加到图像

把上面的for循环改成

initFrame = ax.contourf(f(x,y)) 
cbar      = fig.colorbar(initFrame)
for i in range(10):
    x       += 1
    curVals  = f(x, y)
    vmax     = np.max(curVals)      
    vmin     = np.min(curVals)      
    levels   = np.linspace(vmin, vmax, 200, endpoint = True)
    frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
    cbar.set_clim(vmin = vmin, vmax = vmax)
    cbar.draw_all()
    frames.append(frame.collections + [cbar])

问题:这引发了

AttributeError: 'Colorbar' object has no attribute 'set_visible'

3。在自己的轴上绘图

问题:colorbar 没有更新。

 #!/usr/bin/env python
 """
 An animated image
 """
 import numpy as np
 import matplotlib.pyplot as plt
 import matplotlib.animation as animation

 fig = plt.figure()
 ax1 = fig.add_subplot(121)
 ax2 = fig.add_subplot(122)


 def f(x, y):
     return np.exp(x) + np.sin(y)

 x = np.linspace(0, 1, 120)
 y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

 frames = []

 for i in range(10):
     x       += 1
     curVals  = f(x, y)
     vmax     = np.max(curVals)
     vmin     = np.min(curVals)
     levels   = np.linspace(vmin, vmax, 200, endpoint = True)
     frame    = ax1.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
     cbar     = fig.colorbar(frame, cax=ax2) # Colorbar does not update
     frames.append(frame.collections)

 ani = animation.ArtistAnimation(fig, frames, blit=False)

 plt.show()

2. 和 4. 的组合。

问题:colorbar 是不变的。

here 发布了一个类似的问题,但看起来 OP 对固定的colorbar 很满意。

【问题讨论】:

  • 您与 ArtistAnimation 的关系如何?我不知道该怎么做,但我可以给你一个使用 FuncAnimation 的例子......
  • 据我记得,我开始使用 ArtistAnimation,因为我在使用 FuncAnimation 为一些文本作为标题设置动画时遇到了问题。除此之外,我愿意切换回 FuncAnimation。一个例子将不胜感激:)

标签: python animation matplotlib colorbar


【解决方案1】:

虽然我不确定如何专门使用ArtistAnimation 执行此操作,但使用FuncAnimation 相当简单。如果我对您的“幼稚”版本 1 进行以下修改,它就可以工作。

修改版本 1

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig = plt.figure()
ax = fig.add_subplot(111)

# I like to position my colorbars this way, but you don't have to
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')

def f(x, y):
    return np.exp(x) + np.sin(y)

x = np.linspace(0, 1, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

frames = []
for i in range(10):
    x       += 1
    curVals  = f(x, y)
    frames.append(curVals)

cv0 = frames[0]
cf = ax.contourf(cv0, 200)
cb = fig.colorbar(cf, cax=cax)
tx = ax.set_title('Frame 0')

def animate(i):
    arr = frames[i]
    vmax     = np.max(arr)
    vmin     = np.min(arr)
    levels   = np.linspace(vmin, vmax, 200, endpoint = True)
    cf = ax.contourf(arr, vmax=vmax, vmin=vmin, levels=levels)
    cax.cla()
    fig.colorbar(cf, cax=cax)
    tx.set_text('Frame {0}'.format(i))

ani = animation.FuncAnimation(fig, animate, frames=10)

plt.show()

主要区别在于我在函数中进行关卡计算和轮廓绘制,而不是创建艺术家列表。颜色条之所以有效,是因为您可以清除前一帧的坐标区并在每一帧重做。

在使用contour 或contourf 时必须执行此重做,因为您不能只动态更改数据。但是,由于您绘制了如此多的轮廓级别并且结果看起来很平滑,我认为您最好使用imshow 代替 - 这意味着您实际上可以只使用同一个艺术家并更改数据,并且颜色条会自动更新.它也快得多!

更好的版本

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig = plt.figure()
ax = fig.add_subplot(111)

# I like to position my colorbars this way, but you don't have to
div = make_axes_locatable(ax)
cax = div.append_axes('right', '5%', '5%')

def f(x, y):
    return np.exp(x) + np.sin(y)

x = np.linspace(0, 1, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

# This is now a list of arrays rather than a list of artists
frames = []
for i in range(10):
    x       += 1
    curVals  = f(x, y)
    frames.append(curVals)

cv0 = frames[0]
im = ax.imshow(cv0, origin='lower') # Here make an AxesImage rather than contour
cb = fig.colorbar(im, cax=cax)
tx = ax.set_title('Frame 0')

def animate(i):
    arr = frames[i]
    vmax     = np.max(arr)
    vmin     = np.min(arr)
    im.set_data(arr)
    im.set_clim(vmin, vmax)
    tx.set_text('Frame {0}'.format(i))
    # In this version you don't have to do anything to the colorbar,
    # it updates itself when the mappable it watches (im) changes

ani = animation.FuncAnimation(fig, animate, frames=10)

plt.show()

【讨论】:

  • 谢谢,这解决了我的问题。此示例中的重叠文本也没有问题(这也是我更改为ArtistAnimation 的原因)。使用imshow 当然会更好,但在我的情况下,由于非矩形网格,我坚持使用contourf。
  • @Ajean,您知道如何使用对数色标 (norm=matplotlib.colors.LogNorm()) 和固定 vmin/vmax 值(固定颜色条)做同样的事情吗?
  • @TheoryX 我相信如果您希望 LogNorm(以及因此颜色映射)具有固定的 vmin 和 vmax,您需要做的就是将其传递给对 imshow 的初始调用,然后不要'不要在动画函数中调整clim。不过,这会导致一个固定的颜色条,所以这个特定问题所追求的并不完全是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 2015-05-18
  • 2022-01-13
  • 2019-04-30
相关资源
最近更新 更多