matplotlib [巨蟒]:在讲解动画例子帮助 [英] matplotlib [python] : help in explaining an animation example

查看:124
本文介绍了matplotlib [巨蟒]:在讲解动画例子帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有圣诞快乐,

有人能请解释我如何code ++工程的下面的示例(http://matplotlib.sourceforge.net/examples/animation/random_data.html)?

Could someone please explain me how the following sample of code works (http://matplotlib.sourceforge.net/examples/animation/random_data.html) ?

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


timeline = [1,2,3,4,5,6,7,8,9,10] ;
metric = [10,20,30,40,50,60,70,80,90,100] ;

fig = plt.figure()
window = fig.add_subplot(111)
line, = window.plot(np.random.rand(10))

def update(data):
    line.set_ydata(data)
    return line,

def data_gen():
    while True:
        yield np.random.rand(10)


ani = animation.FuncAnimation(fig, update, data_gen, interval=5*1000)
plt.show()

在我特别想以更新的列表中使用列表(公制)。
个问题是,FuncAnimation是使用发电机,如果我没有记错的话,但是,我怎样才能使它工作?

In particular, I would like to use lists ("metric") in order to update the list. Th problem is that FuncAnimation is using generators if I am not mistaken, but, how can I make it work ?

感谢您。

推荐答案

您可以养活 FuncAnimation 与可迭代的不只是一台发电机。
从文档

You can feed FuncAnimation with any iterable not just a generator. From docs:

类matplotlib.animation.FuncAnimation(图,FUNC,帧=无,
  =的init_func无,fargs =无,save_count =无,** kwargs)

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs)

通过重复调用一个函数func,传递使动画
  (可选)论据fargs。 帧可以是一台发电机,一个可迭代,
  或者帧数
。是的init_func用于绘制清晰的功能
  帧。如果没有给出,图中从第一个项目中的结果
  帧序列将被使用。

Makes an animation by repeatedly calling a function func, passing in (optional) arguments in fargs. frames can be a generator, an iterable, or a number of frames. init_func is a function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used.

因此​​,equivalen code。与名单可以是:

Thus the equivalen code with lists could be:

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

start = [1, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0]

metric =[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65],
         [0.43, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0.55],
         [0.66, 0.75, 0.01, 0.94, 0.72, 0.77, 0.20, 0.66, 0.81, 0.52]
        ]

fig = plt.figure()
window = fig.add_subplot(111)
line, = window.plot(start)

def update(data):
    line.set_ydata(data)
    return line,

ani = animation.FuncAnimation(fig, update, metric, interval=2*1000)
plt.show()

这篇关于matplotlib [巨蟒]:在讲解动画例子帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆