2013-07-09 100 views
6

我无法弄清楚如何在FuncAnimation情节(使用blit)上获得动画标题。基于http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/Python/Matplotlib - Quickly Updating Text on Axes,我已经构建了一个动画,但文本部分不会动画。简单的例子:matplotlib中的动画标题

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_titleax.text

编辑:我发现为什么第一个链接的第二个例子工作;该文本在img部分。如果你使上述1.005 a .99,你会明白我的意思。有可能是一种方式具有边框,不知何故做到这一点...

回答

14

Animating matplotlib axes/tickspython matplotlib blit to axes or sides of the figure?

那么,问题是,在该块传输的背景实际上是保存(行animation.py 792)的animation胆量,它抓住的是在边界框。当您有多个独立动画的轴时,这是有意义的。在你的情况下,你只有一个axes担心,我们想要动画的动画轴边界框外。带着几分猴子修补,公差为伸进MPL的胆量和周围有点戳的水平,并接受最快,dirtyest解决方案,我们可以解决你的问题,因为这样的:

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中有代码可以做到这一点,但我不知道它在哪里。

+0

这工作全速!希望它被包含在matplotlib中,与猴子修补,但效果很好! –

+0

@HenrySchreiner您的编辑应该已被接受。 (我刚刚重新做到了)。如果这解决了你的问题,你可以接受答案(左边的大灰色复选框)。 – tacaswell

+0

理想情况下,应该使用文本边界框(因为它是从动画函数传递的),但由于某种原因它没有被使用(即使我认为它是在艺术家中)。这是现在修复它的一个合理的窍门。 :) 谢谢! –

1

必须调用

plt.draw() 

ttl.set_text(str(n)) 

在这里有一个很简单的例子没有FuncAnimation()“的图形内的文本动画”。尝试一下,你会看到它是否对你有用。

import matplotlib.pyplot as plt 
import numpy as np 
titles = np.arange(100) 
plt.ion() 
fig = plt.figure() 
for text in titles: 
    plt.clf() 
    fig.text(0.5,0.5,str(text)) 
    plt.draw() 
+1

这确实会绘制它,但它会减慢相同的量blit = False。我希望只是重新绘制文本。 –

+0

为什么不简单地避免动画,并用plt.ion()制作自己的动画?有了它,你有很多的控制,你确定你在做什么每一帧...看看http://stackoverflow.com/questions/17444655/dynamically-updating-a-graphed-line-in- python/17480397#17480397 – Pablo

+0

fig = plt.figure() img,= plt.plot(np.sin(vls)) ax = plt.axes() ax.set_xlim([0,2 * 2 * np。 π) 标题= ax.text(0.5,1.005, '',变换= ax.transAxes) plt.ion() 为i的范围(100): img.set_data(VLS,np.sin (vls + .02 *(i%50)* 2 * np.pi)) title.set_text(str(i%50)) plt.draw() –

2

要添加到tcaswell的“猴子修补”解决方案,以下是如何将动画添加到轴刻度标签。具体来说,要动画x轴,请设置ax.xaxis.set_animated(True)并从动画函数返回ax.xaxis

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') 

ax.xaxis.set_animated(True) 

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

def func(n): 
    ttl.set_text(str(n)) 
    vls = np.linspace(0.2*n,0.2*n+2*2*np.pi,100) 
    img.set_data(vls,np.sin(vls)) 
    ax.set_xlim(vls[0],vls[-1]) 
    return img, ttl, ax.xaxis 

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

plt.show() 
1

如果您需要修复的标题,你可以只更新标题:

fig.suptitle() 

参见图API document

+0

如果你打开了blitting,这个工作吗? – bretcj7

+0

@ bretcj7我认为你可以试一试。 – shane