Changing colorbar and title in animated plot

I’d like to change the title and the colorbar for each plot in my Artist animation. But in the final .gif it is only showing the title and colorbar of the last frame.
Any idea what I am missing here?
Thank you for your help

from __future__ import division
 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

class AnimatedGif:
    def __init__(self, vmin=0, vmax=1):
        self.fig, self.ax = plt.subplots()
        self.images = []
        self.vmin = vmin
        self.vmax = vmax
        self.cb = None
        self.title = 'default title'
    
    def add(self, arr, vmin=None, vmax=None, title=None):
        if vmin is None:
            vmin = self.vmin
        if vmax is None:
            vmax = self.vmax
        if title is None:
            title = self.title
        image = plt.imshow(arr.T, vmin=vmin, vmax=vmax, animated=True)
        plt.title(title)
        if self.cb is not None:
            self.cb.remove()
        self.cb = self.fig.colorbar(image)
        self.images.append([image])
    
    def save(self, filename):
        animation = anim.ArtistAnimation(self.fig, self.images)
        animation.save(filename, fps=1)

if __name__ == '__main__':
    arr = np.random.random((3,128,128))
    animated_gif = AnimatedGif()
    animated_gif.add(arr[0], vmin=0, vmax=0.3, title='image 0')
    images = []
    for i in range(1, arr.shape[0]):
        animated_gif.add(arr[i], vmin=i*0.01, vmax=(i+1)*0.3, title='image {}'.format(i))
     
    animated_gif.save('arr.gif')

Explanation of the Issue and Fix:

The problem was that plt.title() sets a single title for the whole plot, so only the last title showed up in the final animation. Similarly, the colorbar was being updated, but it wasn’t part of the animation.

How to fix It:
use self.ax.text instead of plt.title() to set a dynamic title for each frame.
Ensure that both the title and the colorbar were added to each frame’s list of animated objects (artists). This way, both the title and colorbar update properly as the frames change.
main diff

title_artist = self.ax.text(0.5, 1.05, title, transform=self.ax.transAxes, ha=”center”, fontsize=12)

And then add title_artist to self.images.append([image, title_artist]) so that the title updates in every frame.output result

2

test with this code it working like indented and I have tested this

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

class AnimatedGif:
    def __init__(self, vmin=0, vmax=1):
        self.fig, self.ax = plt.subplots()
        self.images = []
        self.vmin = vmin
        self.vmax = vmax
        self.cb = None

    def add(self, arr, vmin=None, vmax=None, title=None):
        if vmin is None:
            vmin = self.vmin
        if vmax is None:
            vmax = self.vmax
        if title is None:
            title = 'default title'
        
        image = self.ax.imshow(arr.T, vmin=vmin, vmax=vmax, animated=True)
        
        # Update the title for this frame
        title_artist = self.ax.text(0.5, 1.05, title, transform=self.ax.transAxes, 
                                    ha="center", fontsize=12, animated=True)

        if self.cb is not None:
            self.cb.remove()  # Removing  previous colorbar
        self.cb = self.fig.colorbar(image, ax=self.ax)  # new colorbar for each frame
        
        # Append the image and title as artists that will be updated in each frame
        self.images.append([image, title_artist])
    
    def save(self, filename):
        animation = anim.ArtistAnimation(self.fig, self.images)
        animation.save(filename, fps=1)

if __name__ == '__main__':
    arr = np.random.random((5, 128, 128))  
    animated_gif = AnimatedGif()
    
    for n in range(1, arr.shape[0] + 1):
        if n % 2 == 1:
            title = f'image {n}'
        else:
            title = f'artist {n}'
        animated_gif.add(arr[n - 1], vmin=(n - 1) * 0.01, vmax=n * 0.3, title=title)
        
    animated_gif.save('arr3.gif')

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật