定义多个地块与一个动画在matplotlib循环

编程入门 行业动态 更新时间:2024-10-11 21:21:17
本文介绍了定义多个地块与一个动画在matplotlib循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

感谢杰克Vanderplas ,我知道如何开始code动画剧情与 matplotlib 。下面是一个示例code:

Thanks to Jake Vanderplas, I know how to start to code an animated plot with matplotlib. Here is a sample code:

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

现在假设我想绘制吨的功能(说,四名这里),用循环的帮助下定义。我做了一些巫术编程,试图了解如何模仿逗号以下行,这里是我得到了什么(不用说,这是行不通的: AttributeError的:'元组'对象没有属性'轴' )。

Suppose now I'd like to plot tons of functions (say four here), defined with the help of a loop. I did some voodoo programming, trying to understand how to mimic the comma following line and here is what I got (needless to say that it does not work: 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()

我的一些问题是:我怎样才能使它发挥作用?奖金(大概链接):是之间的线plt.plot([],[])和行= plt.plot(差别= [] [])?

Some my question is: how can I make it work? Bonus (probably linked): what is the difference between line, = plt.plot([], []) and line = plt.plot([], [])?

感谢

推荐答案

首先,我将张贴您的解决方案,然后一些说明:

First I will post you the solution, then some explanations:

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)] def init(): for line in lines: line.set_data([], []) return lines def animate(i): for j,line in enumerate(lines): line.set_data([0, 2], [10 * j,i]) return lines anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=True) plt.show()

说明:

  • 行= plt.plot([],[])通过指定 plt.plot 来的veriable 行。
  • 行= plt.plot([],[])刚分配整个名单(只有一个元素的)。
  • line, = plt.plot([], []) assign the first element of the list returned by plt.plot to the veriable line.
  • line = plt.plot([], []) just assign the whole list (of only one element).
  • 替代行= [plt.plot([],[])[0] _范围内的(N)] 你可以这样做 =行plt.plot(*([[] []] * N))只有一个绘图命令。我找到的第一个更具可读性,但品味的问题。

    Alternative to lines = [plt.plot([], [])[0] for _ in range(N)] you can do this lines = plt.plot( *([[], []]*N) ) with only one plot command. I found the first more readable but is matter of taste.

    更多推荐

    定义多个地块与一个动画在matplotlib循环

    本文发布于:2023-05-29 03:39:17,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/335908.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   地块   定义   动画   matplotlib

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!