当前位置: 首页 > 面试题库 >

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

郏志学
2023-03-14
问题内容

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

假设现在我想绘制大量的功能(在这里说四个),这些功能是在循环的帮助下定义的。我做了一些voodoo编程,试图了解如何模仿下面的逗号,这就是我所得到的(不必说它不起作用:)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()

我的问题是: 我该如何运作?红利(可能是关联的):line, = plt.plot([], [])和之间有什么区别line = plt.plot([], [])

谢谢


问题答案:

在下面的解决方案中,我展示了一个更大的示例(还带有 条形图
),可以帮助人们更好地理解在其他情况下应采取的措施。在代码之后,我解释了一些细节并回答了奖金问题。

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

说明

这个想法是绘制所需的内容,然后重用所返回的艺术家(请参阅此处的更多信息)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([], []) 只需分配整个列表(只有一个元素)。


 类似资料:
  • 问题内容: 我知道如何开始用. 以下是示例代码: 假设现在我想绘制成吨的函数(这里是四个),用 一个循环的帮助。我做了一些巫毒编程,试图了解如何 模仿下面的逗号,这里是我得到的(不用说 不起作用:)。 *我的问题是:我怎样才能让它工作?奖金(可能有联系):什么 是’line,=plt.绘图([],[])行=plt.绘图([], [])`? 谢谢 问题答案: 在下面的解决方案中,我展示了一个更大的示

  • 本文向大家介绍Python matplotlib读取excel数据并用for循环画多个子图subplot操作,包括了Python matplotlib读取excel数据并用for循环画多个子图subplot操作的使用技巧和注意事项,需要的朋友参考一下 读取excel数据需要用到xlrd模块,在命令行运行下面命令进行安装 pip install xlrd 表格内容大致如下,有若干sheet,每个sh

  • 我无法制作Matplotlib v.2.0的图例。图2显示在Python2.7中。下面我有一个简单的例子,它在for循环的轴上绘制箭头。 由于在每次迭代中指定标签关键字定义并不能使图例出现,因此我尝试使用条件仅在第一次迭代中设置标签。 在ax.legend()中尝试bbox_to_anchor和loc参数没有效果,所以我忽略了任何图例参数。 有人知道如何让一个图例出现,在标签旁边分别显示红色和黑色

  • 我如何比较用户输入的这些数组呢?

  • 问题内容: 我试图用多个变量在Go中编写一个for循环。 来自javascript世界,我想实现以下目标: 我已经尝试过像这样的“原始翻译”: 但这是行不通的。正确的语法是什么? 非常感谢! 问题答案: 在Go中,您可以像这样循环执行多个变量分配。

  • 问题内容: 我想用matplotlib制作3D动画,但是我不知道该怎么做。这是我的无效代码。 问题答案: 我使用了以下示例http://matplotlib.org/1.4.1/examples/animation/simple_3danim.html 并修改了您的代码: