matplotlib动画线条图保持空白 [英] matplotlib animated line plot stays empty

查看:63
本文介绍了matplotlib动画线条图保持空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循,并对其进行调整以显示已经计算出的数据集,而不是每帧都对函数进行评估,但是会陷入困境.我的数据集随着时间的推移涉及 XY 坐标,包含在列表 satxpossatypos 我试图创建一个动画,以便它跟踪从数据集开头开始的一条线最后,每0.1秒显示1个新点.对我要去哪里哪里有帮助吗?

I am trying to follow the basic animation tutorial located here and adapting it to display an already computed dataset instead of evaluating a function every frame, but am getting stuck. My dataset involves XY coordinates over time, contained in the lists satxpos and satypos I am trying to create an animation such that it traces a line starting at the beginning of the dataset through the end, displaying say 1 new point every 0.1 seconds. Any help with where I'm going wrong?

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

Code here creates satxpos and satypos as lists

fig = plt.figure()
ax = plt.axes(xlim=(-1e7,1e7), ylim = (-1e7,1e7))
line, = ax.plot([], [], lw=2)

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

def animate(i):
    line.set_data(satxpos[i], satypos[i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames = len(satxpos), interval = 1, blit=True)

代码运行无错误,但是生成了一个空白的绘图窗口,其中没有显示点/线,也没有进行动画处理.数据集生成正确并且在静态图中显示良好.

The code runs without errors, but generates a blank plot window with no points/lines displayed and nothing animates. The dataset is generated correctly and views fine in a static plot.

推荐答案

为了跟踪从数据集的开头到结尾的一行",您需要对数组进行索引,以在每个时间步长包含一个以上元素:

In order to "trace a line starting at the beginning of the dataset through the end" you would index your arrays to contain one more element per timestep:

line.set_data(satxpos[:i], satypos[:i])

(请注意:!)

代码中的其他所有内容看起来都不错,因此通过上述操作,您应该得到并扩展线图.然后,您可能希望将 interval 设置为大于1的值,因为这将意味着1毫秒的时间步长(可能有点太快了).我想使用 interval = 40 可能是一个不错的开始.

Everything else in the code looks fine, such that with the above manipulation you should get and extending line plot. You might then want to set interval to something greater than 1, as that would mean 1 millesecond timesteps (which might be a bit too fast). I guess using interval = 40 might be a good start.

这篇关于matplotlib动画线条图保持空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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