matplotlib动画多个数据集 [英] matplotlib animation multiple datasets

查看:98
本文介绍了matplotlib动画多个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在matplotlib动画中同时绘制多个数据集。这可能吗?每个数据集都是(x,y)坐标的数组,所以我想通过动画来循环遍历(x,y)坐标,并不断更新直到数组的结尾。对于单个数据集来说很简单,但是我很难做到一个以上。

I have multiple datasets that I want to plot at the same time in a matplotlib animation. Is this possible? Each dataset is an array of (x,y) co-ordinates, so I want to be to animation to cycle through (x,y) co-ordinates, and continuously update until the end of the array. It's straight forward for a single dataset, but I'm having trouble doing it more than one. Any help would be much appreciated.

这是我的问题;

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

def update_line(num, data, line):
    line.set_data(data[...,:num])
    return line,

fig1 = plt.figure()

data1 = np.random.rand(2, 25)
data2 = np.random.rand(2, 25)
l, = plt.plot([], [], 'r-')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),interval=50, blit=False)

plt.show()

这仅适用于绘制第一个数据集,但是我不知道如何将data1和data2一起绘制(将来,我希望将10个数据集放在相同的动画)。我只是不太了解update_line函数的功能,如果有人可以给我答案,那么我脑海中就会浮现很多东西。

This works fine for plotting just the first dataset, however I don't know how to plot both data1 and data2 together (and in the future I would like to have 10 datasets on the same animation). I just don't really understand update_line function in doing, if somebody could give me a answer to this it would clear up a lot of things in my mind.

推荐答案

动画功能每帧仅调用一次,因此您必须确保在一次调用中更新了两个图。

the animation function is only called once per frame, so you have to make sure you're updating both of your plots in that one single call.

例如:

def update_lines(num, data1, data2, line1, line2):
    line1.set_data(data1[...,:num])
    line2.set_data(data2[...,:num])
    return line1,line2

data1 = np.random.rand(2, 25)
data2 = np.random.rand(2, 25)

fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax2 = fig1.add_subplot(122)
l1, = ax1.plot([], [], 'r-')
l2, = ax2.plot([], [], 'g-')
for ax in (ax1, ax2):
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_xlabel('x')
    ax.set_title('test')
line_ani = animation.FuncAnimation(fig1, update_lines, 25, fargs=(data1, data2, l1, l2),interval=50, blit=False)

这篇关于matplotlib动画多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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