在matplotlib中使用for循环定义要绘制动画的多个绘图

cjo*_*sen 8 python animation matplotlib

感谢Jake Vanderplas,我知道如何开始编写动画情节matplotlib.这是一个示例代码:

from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()

ax = plt.axes(xlim=(0, 2), ylim=(0, 100))

line, = plt.plot([], [])

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

def animate(i):
    line.set_data([0, 2], [0,i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

假设现在我想在循环的帮助下绘制大量函数(比如说四个).我做了一些伏都教程序,试图理解如何模仿下面的逗号,这就是我得到的(不用说它不起作用:) AttributeError: 'tuple' object has no attribute 'axes'.

from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()

ax = plt.axes(xlim=(0, 2), ylim=(0, 100))

line = []
N = 4

for j in range(N):
    temp, = plt.plot([], [])
    line.append(temp)

line = tuple(line)

def init():
    for j in range(N):
        line[j].set_data([], [])
    return line,

def animate(i):
    for j in range(N):
        line[j].set_data([0, 2], [10 * j,i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

我的一些问题是:我怎样才能让它发挥作用?奖金(可能挂钩):line, = plt.plot([], [])和之间有什么区别line = plt.plot([], [])

谢谢

Alv*_*tes 18

在下面的解决方案中,我展示了一个更大的例子(也有条形图),可以帮助人们更好地理解其他案例应该做些什么.在代码之后我解释一些细节并回答奖金问题.

import matplotlib
matplotlib.use('Qt5Agg') #use Qt5 as backend, comment this line for default backend

from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()

ax = plt.axes(xlim=(0, 2), ylim=(0, 100))

N = 4
lines = [plt.plot([], [])[0] for _ in range(N)] #lines to animate

rectangles = plt.bar([0.5,1,1.5],[50,40,90],width=0.1) #rectangles to animate

patches = lines + list(rectangles) #things to animate

def init():
    #init lines
    for line in lines:
        line.set_data([], [])

    #init rectangles
    for rectangle in rectangles:
        rectangle.set_height(0)

    return patches #return everything that must be updated

def animate(i):
    #animate lines
    for j,line in enumerate(lines):
        line.set_data([0, 2], [10 * j,i])

    #animate rectangles
    for j,rectangle in enumerate(rectangles):
        rectangle.set_height(i/(j+1))

    return patches #return everything that must be updated

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

说明

我们的想法是绘制您需要的内容,然后重复使用返回的艺术家(请参阅此处)matplotlib.这是通过首先绘制您想要的虚拟草图并保持对象matplotlib为您提供的.然后在您的initanimate函数上,您可以更新需要设置动画的对象.

请注意,plt.plot([], [])[0]我们得到一个单行艺术家,因此我收集它们[plt.plot([], [])[0] for _ in range(N)].另一方面,plt.bar([0.5,1,1.5],[50,40,90],width=0.1)返回一个可以为矩形艺术家迭代的容器.list(rectangles)只需将此容器转换为要连接的列表lines.

我将线条与矩形分开,因为它们的更新方式不同(并且是不同的艺术家)但是initanimate返回所有这些线条.

回答奖金问题:

  1. line, = plt.plot([], [])将返回的列表的第一个元素分配给plt.plotveriable line.
  2. line = plt.plot([], []) 只分配整个列表(只有一个元素).

  • 你好,我可以问一个问题来扩展这个吗?在您的示例中,您有多个“lines”对象,但是如果我想更新“lines”和“image”对象(例如更新点的位置并更新动画中的“matshow()”数组,会发生什么。可以我把这两个放在同一个元组中? (2认同)