1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Changing colorbar and title in animated plot

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    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, vmin=i*0.01, vmax=(i+1)*0.3, title='image {}'.format(i))

    animated_gif.save('arr.gif')

    Continue reading...

Compartilhe esta Página