Search code examples
pythonmatplotlibmatplotlib-animation

Matplotlib animation


I am trying to create an ABC logo and animate it with matplotlib, I have got all the phases by changing the delta index I am able to get all x and y coordinates but I am not able to animate from one phase to another .below is the link of what I am trying to do. https://www.youtube.com/watch?v=sL0FhqGtV4U&ab_channel=AustralianTelevisionArchive

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math
import numpy as np
from matplotlib.animation import FuncAnimation


fig = plt.figure()
axis = plt.axes(xlim =(-110, 110),
                ylim =(-110, 110))
delta = [0, 3.14 / 8, 3.14 / 4, 3 * 3.14 / 8, 3.14 / 2, 5 * 3.14 / 8, 3 * 3.14 / 8, 7 * 3.14 / 8, 3.14]

def dS(delta):
    A = 100
    B = 100
    a = 1
    b = 3
    # delta = 3.14/4
    t = 0
    sinVals = []
    cosVals = []
    for i in range(0, 1000):
        t += 0.01
        # Apply Lissajous Parametric Equations
        sinVals.append(A * math.sin(a * t + delta))
        cosVals.append(B * math.sin(b * t))
    return sinVals,cosVals
    
sinVals,cosVals=deltaShift.dS(delta[1])


line, = axis.plot(sinVals, cosVals)
plt.show()

Solution

  • This will produce what you want

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    import numpy as np
    from matplotlib.animation import FuncAnimation
    
    
    pi = np.pi
    deltas = np.linspace(0,8,32) / pi
    
    def dS(delta):
        
        A = 100
        B = 100
        a = 1
        b = 3
        # delta = 3.14/4
        t = 0
        sinVals = []
        cosVals = []
        for i in range(0, 1000):
            t += 0.01
            # Apply Lissajous Parametric Equations
            sinVals.append(A * math.sin(a * t + delta))
            cosVals.append(B * math.sin(b * t))
        return sinVals,cosVals
        
    def make_plot(delta):
        
        ax.clear()
        ax.set_xlim(-110, 110)
        ax.set_ylim(-110, 110)
        sinVals,cosVals=dS(delta)
        line, = ax.plot(sinVals, cosVals)
    
    fig,ax = plt.subplots()
    
    ani = animation.FuncAnimation(fig, make_plot, deltas, blit = False)
    ani.save('test.gif', writer='pillow', fps=16)
    

    The output gif file is

    gif