2014-01-29 72 views
8

我想绘制两个使用Matplotlib动画库的旋转椭圆,并且设法让它工作(或多或少)。问题是正在渲染的第一帧没有更新,所以当我在画布中获得两个旋转椭圆时,我也有椭圆在原始位置/方向。看看我的一段简单的代码:Matplotlib动画:使用blit时第一帧保留在画布中

import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse 
from matplotlib import animation 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.2, angle=60) 
e2 = Ellipse(xy=(0.8, 0.8), width=0.5, height=0.2, angle=100) 

def init(): 
    ax.add_patch(e1) 
    ax.add_patch(e2) 
    return [e1,e2] 

def animate(i): 
    e1.angle = e1.angle + 0.5 
    e2.angle = e2.angle + 0.5 
    return [e1,e2] 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True) 
plt.show() 

任何想法如何解决这个问题?我当然可以关闭blit,但这会让它非常慢,所以这不是一个真正的选择。

谢谢!

编辑:最后的(工作)代码

import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse 
from matplotlib import animation 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.2, angle=60) 
e2 = Ellipse(xy=(0.8, 0.8), width=0.5, height=0.2, angle=100) 
ax.add_patch(e1) 
ax.add_patch(e2) 

def init(): 
    e1.set_visible(False) 
    e2.set_visible(False) 
    return [e1,e2] 

def animate(i): 
    if i == 1: 
     e1.set_visible(True) 
     e2.set_visible(True) 
    e1.angle = e1.angle + 0.5 
    e2.angle = e2.angle + 0.5 
    return [e1,e2] 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True) 
plt.show() 

回答

3

试试这个:

import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse 
from matplotlib import animation 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.2, angle=60) 
e2 = Ellipse(xy=(0.8, 0.8), width=0.5, height=0.2, angle=100) 


def init(): 
    return [ax] 

def animate(i): 
    if i==0: 
     ax.add_patch(e1) 
     ax.add_patch(e2)  
    e1.angle = e1.angle + 0.5 
    e2.angle = e2.angle + 0.5 
    return [e1,e2] 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True) 
plt.show() 

Screen-shot showing the correct behavior

试试这个另一种方法(不是我用仅仅是出于一个椭圆测试,它也呈现在这里罚款):

import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse 
from matplotlib import animation 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.2, angle=60) 
ax.add_patch(e1) 

def init(): 
    e1.set_visible(False) 
    return e1, 

def animate(i): 
    if i==0: 
     e1.set_visible(True) 
    e1.angle = e1.angle + 0.5 
    return e1, 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True) 
plt.show() 
+0

没有,没有工作,结果和以前一样。 – MPA

+0

你确定吗?我复制了昨天发布的代码,并且在这里顺利运行。 (Matplotlib 1.2.1) –

+0

是的,我复制粘贴你的代码,仍然像以前一样。我也在v1.2.1上。 – MPA

3

建议的答案看起来像一个黑客。你隐藏了一帧的补丁,然后显示下一个补丁,因为没有韵或理由。另外,我注意到每当调整图的大小时都会调用init函数,然后在调整所提出的解决方案之后使椭圆不可见。

我认为正确的做法是在Ellipse对象上设置animated=True。请参阅matplotlib animation documentation。这里有一些代码适用于我:

import matplotlib.pyplot as plt 
from matplotlib.patches import Ellipse 
from matplotlib import animation 

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
e1 = Ellipse(xy=(0.5, 0.5), width=0.5, height=0.2, angle=60, animated=True) 
e2 = Ellipse(xy=(0.8, 0.8), width=0.5, height=0.2, angle=100, animated=True) 
ax.add_patch(e1) 
ax.add_patch(e2) 

def init(): 
    return [e1, e2] 

def animate(i): 
    e1.angle = e1.angle + 0.5 
    e2.angle = e2.angle + 0.5 
    return [e1, e2] 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1, blit=True) 
plt.show()