动画散点图

时间:2019-08-23 09:32:01

标签: python matplotlib animation

我想用python做一个动画,逐点表示每个点。我一直在这样做,但它不起作用。有什么帮助吗? 我尝试了两种不同的方法。

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 ()

第二种方式:

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 ()

使用这些代码,我得到了包含所有要点的整个图。我想以动画的方式逐点填充图形。

2 个答案:

答案 0 :(得分:1)

编辑:我在普通的Python Shell中对其进行了测试,它在红点上绘制了黑点,但是Jupyter在红点后面绘制了黑点,因此它需要以不同的顺序排列这些线-第一个红点,下一个空点为黑点。

ax.plot(n, an, 'ro', markersize=2) # red dots
pt, = ax.plot([], [], 'ko', markersize=2)

首先:我收到错误消息

 TypeError: 'Line2D' object is not iterable. 

全部是因为()不创建元组-您必须使用逗号,在以下位置创建元组 return pt,fargs=(pt,)


问题是因为您在开始使用时绘制了所有点

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

然后它在相同的位置绘制点,这样您就看不到动画了。

如果您使用其他颜色-即。 red

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

然后您将在红色点上看到黑色点的动画。

enter image description here

或者删除这条线,它将在空窗口中绘制点。

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, 'ro', markersize=2) # red dots

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 ()

在第二个代码中,您遇到相同的问题,并且还忘记了i中的def animate(i, pt):

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, 'ro', markersize=2)

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

def animate(i, 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 ()

答案 1 :(得分:0)

以下将起作用

%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 ()