【问题标题】:How to update subplot's colorbar during animation?如何在动画期间更新子图颜色条?
【发布时间】:2021-07-16 03:51:15
【问题描述】:

下面是我的带有子图的示例动画。我有两个颜色条:本地和全局。我找到了一种在动画期间更新全局颜色条的方法。但是,我找不到更新本地的方法。

在当前的实现中,在每次迭代中我都会得到一个额外的颜色条(参见attached)。给定代码结构,有没有办法清除/更新它?

import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import math
from scipy.interpolate import RectBivariateSpline
import matplotlib.animation as animation

# Animation with subplots

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15,15), subplot_kw=dict(projection='polar'))

def myValues(*args):
    azimuths = np.radians(np.linspace(0, 360, 20))
    zeniths  = np.arange(0, 150, 10)

    rho, theta = np.meshgrid(zeniths, azimuths)
    values     = np.random.random((azimuths.size, zeniths.size))
    
    return theta, rho, values

# ---------------------------------Sub-plot #1 ---------------------------------
theta, rho, values = myValues()
im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
fig.colorbar(im1, ax=axes.flat[0]) # local colorbar

# ---------------------------------Sub-plot #2 ---------------------------------
theta, rho, values = myValues()
im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')

# ---------------------------------Sub-plot #3 ---------------------------------
theta, rho, values = myValues()
im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')

# ---------------------------------Sub-plot #4 ---------------------------------
theta, rho, values = myValues()
im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')

# Global colorbar
fig.subplots_adjust(right=0.94)
cax = fig.add_axes([0.95, 0.15, 0.02, 0.7])
gcb = fig.colorbar(im4, cax=cax)

# This function is called periodically from FuncAnimation
def updateFig(*args):
    
    global im1, im2, im3, im4, lcb, gcb
    
    # ---------------------------------Sub-plot #1 ---------------------------------
    theta, rho, values = myValues()
    
    for c in im1.collections:
        c.remove()  # removes only the contours, leaves the rest intact
    
    im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
    
    # How to update this local colorbar?
    lcb = fig.colorbar(im1, ax=axes.flat[0])

    # ---------------------------------Sub-plot #2 ---------------------------------
    theta, rho, values = myValues()
    
    for c in im2.collections:
        c.remove()
    
    im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')

    # ---------------------------------Sub-plot #3 ---------------------------------
    theta, rho, values = myValues()
    
    for c in im3.collections:
        c.remove()
    
    im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')

    # ---------------------------------Sub-plot #4 ---------------------------------
    theta, rho, values = myValues()
    
    for c in im4.collections:
        c.remove()
    
    im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')
    
    # Update global colorbar
    cax.cla()
    gcb = fig.colorbar(im4, cax=cax)
    
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, updateFig)
plt.show()

这篇文章 https://stackoverflow.com/questions/39472017/how-to-animate-the-colorbar-in-matplotlib 似乎是相关的,但我无法采用建议的用子图解决我的情况。

【问题讨论】:

    标签: python animation subplot colorbar contourf


    【解决方案1】:

    这是我的解决方法:

    # Animation with subplots
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15,15), subplot_kw=dict(projection='polar'))
    
    def myValues(*args):
        azimuths = np.radians(np.linspace(0, 360, 20))
        zeniths  = np.arange(0, 150, 10)
    
        rho, theta = np.meshgrid(zeniths, azimuths)
        values     = np.random.random((azimuths.size, zeniths.size))
        
        return theta, rho, values
    
    # ---------------------------------Sub-plot #1 ---------------------------------
    theta, rho, values = myValues()
    im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
    
    #Local colorbar
    fig.subplots_adjust(right=0.47)
    lcb_cax = fig.add_axes([0.5, 0.53, 0.02, 0.35])
    lcb = fig.colorbar(im1, cax=lcb_cax)
    
    # ---------------------------------Sub-plot #2 ---------------------------------
    theta, rho, values = myValues()
    im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')
    
    # ---------------------------------Sub-plot #3 ---------------------------------
    theta, rho, values = myValues()
    im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')
    
    # ---------------------------------Sub-plot #4 ---------------------------------
    theta, rho, values = myValues()
    im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')
    
    # Global colorbar
    fig.subplots_adjust(right=0.94)
    cax = fig.add_axes([0.95, 0.15, 0.02, 0.7])
    gcb = fig.colorbar(im4, cax=cax)
    
    # This function is called periodically from FuncAnimation
    def updatefig(*args):
        
        global im1, im2, im3, im4, lcb, gcb
        
        # ---------------------------------Sub-plot #1 ---------------------------------
        theta, rho, values = myValues()
        
        for c in im1.collections:
            c.remove()  # removes only the contours, leaves the rest intact
        
        im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
        
        # How to update this local colorbar?
        lcb_cax.cla()
        lcb = fig.colorbar(im1, cax=lcb_cax)
    
        # ---------------------------------Sub-plot #2 ---------------------------------
        theta, rho, values = myValues()
        
        for c in im2.collections:
            c.remove()
        
        im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')
    
        # ---------------------------------Sub-plot #3 ---------------------------------
        theta, rho, values = myValues()
        
        for c in im3.collections:
            c.remove()
        
        im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')
    
        # ---------------------------------Sub-plot #4 ---------------------------------
        theta, rho, values = myValues()
        
        for c in im4.collections:
            c.remove()
        
        im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')
        
        # Update global colorbar
        gcb_cax.cla()
        gcb = fig.colorbar(im4, cax=gcb_cax)
        
    # Set up plot to call animate() function periodically
    ani = animation.FuncAnimation(fig, updatefig)
    
    plt.show()
    

    很可能有更好的方法来做到这一点。如果你知道,请告诉我!

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多