在 Python/Matplotlib 中动画“增长”线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28074461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:42:17  来源:igfitidea点击:

Animating "growing" line plot in Python/Matplotlib

pythonanimationgraphicsmatplotlib

提问by Blink

I want to produce a set of frames that can be used to animate a plot of a growing line. In the past, I have always used plt.draw() and set_ydata() to redraw the y-data as it changed over time. This time, I wish to draw a "growing" line, moving across the graph with time. Because of this, set_ydata doesn't work (xdata is changing length). For example,

我想生成一组可用于为不断增长的线图制作动画的帧。过去,我一直使用 plt.draw() 和 set_ydata() 来重绘 y 数据,因为它会随着时间的推移而变化。这一次,我想画一条“增长”线,随着时间在图形上移动。因此, set_ydata 不起作用(xdata 正在改变长度)。例如,

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure()
for n in range(len(x)):
    plt.plot(x[:n], y[:n], color='k')
    plt.axis([0, 10, 0, 1])
    plt.savefig('Frame%03d.png' %n)

While this works, it becomes very slow as it scales. Is there a faster way to do this?

虽然这有效,但随着它的扩展,它变得非常缓慢。有没有更快的方法来做到这一点?

采纳答案by Joe Kington

A couple of notes:

一些注意事项:

First off, the reason that things become progressively slower is that you're drawing more and more and more overlapping lines in the same position.

首先,事情变得越来越慢的原因是您在同一位置绘制了越来越多的重叠线。

A quick fix is to clear the plot each time:

一个快速的解决方法是每次都清除绘图:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure()
for n in range(len(x)):
    plt.cla()
    plt.plot(x[:n], y[:n], color='k')
    plt.axis([0, 10, 0, 1])
    plt.savefig('Frame%03d.png' %n)

Better yet, however, update both the x and y data at the same time:

然而,更好的是,同时更新 x 和 y 数据:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')

for n in range(len(x)):
    line.set_data(x[:n], y[:n])
    ax.axis([0, 10, 0, 1])
    fig.canvas.draw()
    fig.savefig('Frame%03d.png' %n)

And if you'd like to use the animation module (side note: blit=Truemay not work properly on some backends (e.g. OSX), so try blit=Falseif you have issues):

如果你想使用动画模块(旁注:blit=True在某些后端(例如 OSX)上可能无法正常工作,所以blit=False如果你有问题,请尝试):

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

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
line, = ax.plot(x, y, color='k')

def update(num, x, y, line):
    line.set_data(x[:num], y[:num])
    line.axes.axis([0, 10, 0, 1])
    return line,

ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line],
                              interval=25, blit=True)
ani.save('test.gif')
plt.show()

enter image description here

在此处输入图片说明