简体   繁体   English

动画散点图

[英]Animated Scatter Plot

I would like to do an animation in python representing each point, dot by dot. 我想在python中制作动画,逐点表示每个点。 I am doing it as I always have done it, but it does not work. 我一直在这样做,但它不起作用。 Any help? 有什么帮助吗? I tried 2 different ways. 我尝试了2种不同的方式。

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. 我想以动画的方式逐点填充图形。

EDIT: I tested it in normal Python Shell and it draws black dots on red dots but Jupyter draws black dots hidden behind red dots so it needs these lines in different order - first red dots, next empty plot for black dots. 编辑:我在正常的Python Shell中对其进行了测试,它在红色点上绘制了黑点,但是Jupyter在红色点后面绘制了黑点,因此它需要以不同的顺序排列这些线-第一个红色点,下一个空白点为黑色点。

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

First: I get error message 第一:我收到错误消息

 TypeError: 'Line2D' object is not iterable. 

All because () doesn't create tuple - you have to use comma , to create tuple in return pt, and fargs=(pt,) 这一切都是因为()不创建的元组-你必须使用逗号,在创建的元组return pt,fargs=(pt,)


Problem is because you draw all point at start using 问题是因为您在开始使用时绘制了所有点

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

and later it draws dots in the same places so you don't see animation. 后来它在相同的位置绘制点,因此您看不到动画。

If you use different color - ie. 如果您使用其他颜色-即。 red

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

then you will see animation of black points on red points. 那么您将在红色点上看到黑色点的动画。

在此处输入图片说明

Or remove this line and it will draw dots in empty window. 或删除此线,它将在空窗口中绘制点。

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

In second code you have the same problems and you also forgot i in def animate(i, pt): 在第二个代码中,您遇到同样的问题,并且在def animate(i, pt):也忘记了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 ()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM