Matplotlib动画:如何动态扩展x限制? [英] Matplotlib Animation: how to dynamically extend x limits?

查看:88
本文介绍了Matplotlib动画:如何动态扩展x限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的动画情节,如下所示:

I have a simple animation plot like so:

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

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 100), ylim=(0, 100))
line, = ax.plot([], [], lw=2)

x = []
y = []


# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,


# animation function.  This is called sequentially
def animate(i):
    x.append(i + 1)
    y.append(10)
    line.set_data(x, y)
    return line,


# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()

现在,这行得通,但是我希望它像这里的子图之一一样扩展 http://www.roboticslab.ca/matplotlib-animation/ ,其中x轴动态扩展以适应

Now, this works okay, but I want it to expand like one of the subplots in here http://www.roboticslab.ca/matplotlib-animation/ where the x-axis dynamically extends to accommodate the incoming data points.

我该怎么做?

推荐答案

我遇到了这个问题(但对于set_ylim),我对@ImportanceOfBeingErnest的评论进行了一些试验和错误,这就是我得到的内容,适用于@ nz_21问题。

I came across this problem (but for set_ylim) and I had some trial and error with the comment of @ImportanceOfBeingErnest and this is what I got, adapted to @nz_21 question.

def animate(i):
    x.append(i + 1)
    y.append(10)
    ax.set_xlim(min(x), max(x)) #added ax attribute here
    line.set_data(x, y)

    return line, 

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=500, interval=20)

实际上是在网络上引用由@ nz_21提供类似的解决方案。

Actually in the web quoted by @nz_21 there is a similar solution.

这篇关于Matplotlib动画:如何动态扩展x限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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