【问题标题】:Python matplotlib animation is lagging when I continue drwign lines当我继续画线时,Python matplotlib 动画滞后
【发布时间】:2022-01-04 20:19:34
【问题描述】:

我试图从一个点开始画一条长线,然后在一定时间后创建新点,这将是该线的下一个点。所以它应该看起来像一条路径。但是我遇到了一个问题,当线路变长时,计算机出现计算问题并且开始滞后。我认为,该程序应该在此期间以相同的速度运行,除非它在磁盘上使用成倍增加的空间或再次绘制相同的点。不知道是什么原因造成了这些困难,请您提出一些方法,如何改善它?

这是我的代码示例:

import matplotlib.pyplot as plt
import random
import matplotlib.animation as animation

CELLS: list = []
# number of cells
NUM_CELLS: int = 100

# how big is the move
MAX_STEP: int = 10

# how many ms does it take to update → higher means slower
SPEED: int = 10


fig = plt.figure()
plt.xlim(0, 1000)
plt.ylim(0, 1000)
plt.axis("off")

def create_cell():
    y_coord = random.randint(0, 1000)
    x_coord = random.randint(0, 1000)
    CELLS.append([x_coord, y_coord])
    
def move(self):
    new_cells = []
    for cell in CELLS:
        x_coord1 = cell[0]
        y_coord1 = cell[1]
        
        move_x = random.randint(-1, 1)
        
        move_y = random.randint(-1, 1)

        x_coord2 = cell[0] + (MAX_STEP * move_x)
        y_coord2 = cell[1] + (MAX_STEP * move_y)
        x_coords = [x_coord1, x_coord2]
        y_coords = [y_coord1, y_coord2]
        
        plt.plot(x_coords, y_coords, color="blue")

        new_cells.append([x_coord2, y_coord2])
    
    CELLS.clear()
    for point in new_cells:
        CELLS.append(point)

for i in range(NUM_CELLS):
    create_cell()

animator = animation.FuncAnimation(fig, move, frames=100, interval = SPEED)

plt.show()

【问题讨论】:

标签: python performance matplotlib


【解决方案1】:

如果你只使用一次plot,然后使用set_data更新数据,它会更快。

我已经在你的 sn-p 中更改了 3 行,现在似乎运行得很快。

import matplotlib.pyplot as plt
import random
import matplotlib.animation as animation

CELLS: list = []
# number of cells
NUM_CELLS: int = 100

# how big is the move
MAX_STEP: int = 10

# how many ms does it take to update → higher means slower
SPEED: int = 10


fig = plt.figure()
plt.xlim(0, 1000)
plt.ylim(0, 1000)
plt.axis("off")

line, = plt.plot([], [], color="blue") #changed

def create_cell():
    
    y_coord = random.randint(0, 1000)
    x_coord = random.randint(0, 1000)
    CELLS.append([x_coord, y_coord])

def init():
    line.set_data([], [])
    return line,

def move(self):
    new_cells = []
    for cell in CELLS:
        x_coord1 = cell[0]
        y_coord1 = cell[1]
        
        move_x = random.randint(-1, 1)
        
        move_y = random.randint(-1, 1)

        x_coord2 = cell[0] + (MAX_STEP * move_x)
        y_coord2 = cell[1] + (MAX_STEP * move_y)
        x_coords = [x_coord1, x_coord2]
        y_coords = [y_coord1, y_coord2]

        new_cells.append([x_coord2, y_coord2])
    
    CELLS.clear()
    for point in new_cells:
        CELLS.append(point)
    line.set_data([point[0] for point in new_cells], [point[1] for point in new_cells]) #changed
    return line, #changed

for i in range(NUM_CELLS):
    create_cell()

animator = animation.FuncAnimation(fig, move, init_func=init, frames=100, interval = SPEED) #changed

plt.show()

【讨论】:

  • 我不知道为什么,但是它显示了许多不同方向的线条,而这些线条只是在晃动。这些线条中的任何一条都是实时创建的。
猜你喜欢
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2019-08-13
  • 2014-05-02
  • 2015-04-13
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多