Search code examples
pythonmatplotlibscatter-plotmatplotlib-animation

Animated Scatter Plot


I would like to do an animation in python representing each point, dot by dot. I am doing it as I always have done it, but it does not work. Any help? I tried 2 different ways.

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

%matplotlib nbagg

def mcd(a, b):
    resto = 0
    while(b > 0):
        resto = b
        b = a % b
        a = resto
    return a

N = 1200

n = list (range (N))
an = [1,1]

for i in range (2,N):
    k = i-1
    if mcd (n[i], an[k]) == 1:
        an.append (n[i] + 1 + an[k])
    else:
        an.append (an[k]/mcd (n[i], an[k]))

fig = plt.figure ()
ax = fig.add_subplot (111)
ax.grid (True)
ax.set_xlim(0, N*1.1)
pt, = ax.plot ([],[],'ko', markersize=2)

ax.plot (n,an, 'ko', markersize=2)

def init ():

    pt.set_data([],[])
    return (pt)

def animate (i,pt):

    pt.set_data (n[:i],an[:i])

    return (pt)

ani = FuncAnimation (fig, animate, fargs = (pt), frames=N, init_func=init, interval=50, blit = True)

plt.show ()

And the second way:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

%matplotlib nbagg

def mcd(a, b):
    resto = 0
    while(b > 0):
        resto = b
        b = a % b
        a = resto
    return a

N = 1200

n = list (range (N))
an = [1,1]

for i in range (2,N):
    k = i-1
    if mcd (n[i], an[k]) == 1:
        an.append (n[i] + 1 + an[k])
    else:
        an.append (an[k]/mcd (n[i], an[k]))

xdata, ydata = [],[]

fig = plt.figure ()
ax = fig.add_subplot(111)
ax.grid (True)
pt, = ax.plot ([],[],'ko', markersize=2)

ax.plot (n,an, 'ko', markersize=2)

def init ():
    ax.set_xlim(0, N*1.1)
    pt.set_data([],[])
    return (pt)

def animate (pt):
    xdata.append (n[i])
    ydata.append (an[i])

    pt.set_data (xdata,ydata)

    return (pt)

ani = FuncAnimation (fig, animate, fargs = (pt), frames=N, init_func=init, interval=50, blit = True)

plt.show ()

Using those codes, I get the entire figure with all the points. I would like to fill the graph point by point in an animated way.


Solution

  • The following will work

    %matplotlib nbagg
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    
    def mcd(a, b):
        resto = 0
        while(b > 0):
            resto = b
            b = a % b
            a = resto
        return a
    
    N = 1200
    
    n = list (range (N))
    an = [1,1]
    
    for i in range (2,N):
        k = i-1
        if mcd (n[i], an[k]) == 1:
            an.append (n[i] + 1 + an[k])
        else:
            an.append (an[k]/mcd (n[i], an[k]))
    
    fig = plt.figure ()
    ax = fig.add_subplot (111)
    ax.grid (True)
    ax.set_xlim(0, N*1.1)
    ax.set_ylim(min(an), max(an))
    pt, = ax.plot([],[],'ko', markersize=2)
    
    def init ():
        pt.set_data([], [])
        return pt,
    
    def animate(i):
        pt.set_data (n[:i], an[:i])
        return pt,
    
    ani = FuncAnimation (fig, animate, frames=N, init_func=init, interval=50, blit = True)
    
    plt.show ()
    

    enter image description here