如何更新散点图动画

问题描述 投票:0回答:2

我尝试编写一个简单的脚本,为每个时间步更新散点图

t
。我想做得尽可能简单。但它所做的只是打开一扇我什么也看不见的窗户。窗户就结冰了。这可能只是一个小错误,但我找不到它。

data.dat
的格式为

                x      y
Timestep 1      1      2
                3      1
Timestep 2      6      3
                2      1

(文件仅包含数字)

import numpy as np
import matplotlib.pyplot as plt
import time

# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
    particles = []
    for line in fp:
        line = line.split() 
        if line:
            line = [float(i) for i in line]
            particles.append(line)

T = 100
numbParticles = 2

x, y = np.array([]), np.array([])

plt.ion()
plt.figure()
plt.scatter(x,y)
for t in range(T):
    plt.clf()
    for k in range(numbP):
            x = np.append(x, particles[numbParticles*t+k][0])
            y = np.append(y, particles[numbParticles*t+k][1])
    plt.scatter(x,y)
    plt.draw()
    time.sleep(1)
    x, y = np.array([]), np.array([])
python matplotlib scatter-plot matplotlib-animation
2个回答
5
投票

制作动画最简单、最干净的方法是使用 matplotlib.animation 模块

由于散点图返回一个

matplotlib.collections.PathCollection
,因此更新它的方法是调用其
set_offsets
方法。您可以向其传递形状 (N, 2) 的数组或 N 2 元组的列表 - 每个 2 元组都是一个
(x,y)
坐标。

例如,

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

T = 100
numbParticles = 2
particles = np.random.random((T,numbParticles)).tolist()
x, y = np.array([]), np.array([])

def init():
    pathcol.set_offsets([[], []])
    return [pathcol]

def update(i, pathcol, particles):
    pathcol.set_offsets(particles[i])
    return [pathcol]

fig = plt.figure()
xs, ys = zip(*particles)
xmin, xmax = min(xs), max(xs)
ymin, ymax = min(ys), max(ys)
ax = plt.axes(xlim=(xmin, xmax), ylim=(ymin, ymax))
pathcol = plt.scatter([], [], s=100)

anim = animation.FuncAnimation(
    fig, update, init_func=init, fargs=(pathcol, particles), interval=1000, frames=T, 
    blit=True, repeat=True)
plt.show()

循环位于 FuncAnimation() 内部,所以你看不到它。如果你愿意的话,你可以查看源代码(实际上那里没有循环,而是一个迭代器),但你也可以接受从外部看到的

FuncAnimation(fig, update, interval=1000, frames=100)
只会每1000毫秒调用一次函数
update
并将
i
加 1 直到
frames
给出的数字。


0
投票

我终于找到了解决办法。您只需使用此脚本即可完成此操作。我尽量保持简单:

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

# Helps me to get the data from the file I want to plot
N = 0

# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
    particles = []
    for line in fp:
        line = line.split() 
        particles.append(line)

# Create new Figure and an Axes which fills it.
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=True)
border = 100
ax.set_xlim(-border, border), ax.set_xticks([])
ax.set_ylim(-border, border), ax.set_yticks([])

# particle data
p = 18 # number of particles
myPa = np.zeros(p, dtype=[('position', float, 2)])

# Construct the scatter which we will update during animation
scat = ax.scatter(myPa['position'][:, 0], myPa['position'][:, 1])

def update(frame_number):
    # New positions
    myPa['position'][:] = particles[N*p:N*p+p]

    # Update the scatter collection, with the new colors, sizes and positions.
    scat.set_offsets(myPa['position'])
    increment()

def increment():
    global N
    N = N+1

# Construct the animation, using the update function as the animation director.
animation = FuncAnimation(fig, update, interval=20)

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.