Home > Net >  How to fade curves of an animated gif in matplotlib
How to fade curves of an animated gif in matplotlib

Time:09-15

I have a gif but i want to fade the curves plotted. I have read enter image description here

This code makes the gif for me:

import matplotlib
import imageio
def plot_for_offset(time, value):
    ims = []
    fig, axs = plt.subplots(figsize=(6,2.5))
    for t, v in zip (time, value):
        axs.plot(t, v, lw=1.0, color='k')
        # Used to return the plot as an image rray
        fig.canvas.draw()       # draw the canvas, cache the renderer
        image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
        image  = image.reshape(fig.canvas.get_width_height()[::-1]   (3,))
        ims.append(image)
    return ims
kwargs_write = {'fps':1.0, 'quantizer':'nq'}

import numpy as np                              
time = np.array([[0., 1., 2.], [0., 1., 2.], [0., 1., 2.]])
value = np.array([[0., 10., 20.], [20., 10., 0.], [10., 10., 10.]])
imageio.mimsave('animation.gif', plot_for_offset(time, value), fps=1.0)

Now, I want to fade the curves poped up in the past. I very much appreciate any help.

CodePudding user response:

You can do that by changing the transparency alpha of your plot at each frame of your animation.

See example below using the FuncAnimation function from matplotlib (doc enter image description here

  • Related