在matplotlib动画标题 [英] Animated title in matplotlib

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

问题描述

我无法弄清楚如何让动画标题上FuncAnimation情节的工作(使用的blit)。基于 http://jakevdp.github.io/blog/ 2012/08/18 / matplotlib动画教程/ 和<一个href=\"http://stackoverflow.com/questions/6077017/python-matplotlib-quickly-updating-text-on-axes\">Python/Matplotlib - 快速更新的轴文本,我已经建立了一个动画,但文字部分就不会动画。简单的例子:

I can't figure out how to get an animated title working on a FuncAnimation plot (that uses blit). Based on http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ and Python/Matplotlib - Quickly Updating Text on Axes, I've built an animation, but the text parts just won't animate. Simplified example:

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

vls = np.linspace(0,2*2*np.pi,100)

fig=plt.figure()
img, = plt.plot(np.sin(vls))
ax = plt.axes()
ax.set_xlim([0,2*2*np.pi])
#ttl = ax.set_title('',animated=True)
ttl = ax.text(.5, 1.005, '', transform = ax.transAxes)

def init():
    ttl.set_text('')
    img.set_data([0],[0])
    return img, ttl
def func(n):
    ttl.set_text(str(n))
    img.set_data(vls,np.sin(vls+.02*n*2*np.pi))
    return img, ttl

ani = animation.FuncAnimation(fig,func,init_func=init,frames=50,interval=30,blit=True)

plt.show()

如果的blit = TRUE 被删除,文本显示,但它减缓一路下滑。这似乎失败, plt.title ax.set_title ax.text

If blit=True is removed, the text shows up, but it slows way down. It seems to fail with plt.title, ax.set_title, and ax.text.

编辑:我发现,为什么在第一个链接第二个例子中的工作;该文本是 IMG 部分内。如果你把上面的 1.005 A 0.99 ,你就会明白我的意思。有可能是一种带有边框要做到这一点,不知何故...

I found out why the second example in the first link worked; the text was inside the img part. If you make the above 1.005 a .99, you'll see what I mean. There probably is a way to do this with a bounding box, somehow...

推荐答案

请参见动画matplotlib轴/蜱和<一个href=\"http://stackoverflow.com/questions/14844223/python-matplotlib-blit-to-axes-or-sides-of-the-figure\">python matplotlib的blit到人物的轴或边?

那么,问题是,在胆量动画,其中blit的背景实际上是保存(行792的<一个href=\"https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/animation.py#L792\">animation.py),它抓住的是在的边界框。这使得当你有多个轴被独立动画的感觉。你的情况,你只能有一个不用担心,我们希望外界的边框。带着几分猴子修补,宽容为达到进入MPL的胆量和周围有点戳水平轴的动画东西的,验收最快和dirtyest解决方案,我们可以解决你的问题,因为这样的:

So, the problem is that in the guts of animation where the blit backgrounds are actually saved (line 792 of animation.py), it grabs what is in the axes bounding box. This makes sense when you have multiple axes being independently animated. In your case you only have one axes to worry about and we want to animate stuff outside of the axes bounding box. With a bit of monkey patching, a level of tolerance for reaching into the guts of mpl and poking around a bit, and acceptance of the quickest and dirtyest solution we can solve your problem as such:

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

def _blit_draw(self, artists, bg_cache):
    # Handles blitted drawing, which renders only the artists given instead
    # of the entire figure.
    updated_ax = []
    for a in artists:
        # If we haven't cached the background for this axes object, do
        # so now. This might not always be reliable, but it's an attempt
        # to automate the process.
        if a.axes not in bg_cache:
            # bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
            # change here
            bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
        a.axes.draw_artist(a)
        updated_ax.append(a.axes)

    # After rendering all the needed artists, blit each axes individually.
    for ax in set(updated_ax):
        # and here
        # ax.figure.canvas.blit(ax.bbox)
        ax.figure.canvas.blit(ax.figure.bbox)

# MONKEY PATCH!!
matplotlib.animation.Animation._blit_draw = _blit_draw

vls = np.linspace(0,2*2*np.pi,100)

fig=plt.figure()
img, = plt.plot(np.sin(vls))
ax = plt.axes()
ax.set_xlim([0,2*2*np.pi])
#ttl = ax.set_title('',animated=True)
ttl = ax.text(.5, 1.05, '', transform = ax.transAxes, va='center')

def init():
    ttl.set_text('')
    img.set_data([0],[0])
    return img, ttl

def func(n):
    ttl.set_text(str(n))
    img.set_data(vls,np.sin(vls+.02*n*2*np.pi))
    return img, ttl

ani = animation.FuncAnimation(fig,func,init_func=init,frames=50,interval=30,blit=True)

plt.show()

请注意,当预期的,如果你在你的身材有多个轴,这可能无法正常工作。一个更好的解决办法是扩大 axes.bbox 只是的足以捕捉你的标题+轴的刻度标签。我怀疑是MPL code某个地方要做到这一点,但我不知道它在哪里把我的头顶部。

Note that this may not work as expected if you have more than one axes in your figure. A much better solution is to expand the axes.bbox just enough to capture your title + axis tick labels. I suspect there is code someplace in mpl to do that, but I don't know where it is off the top of my head.

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

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