Matplotlib动画:使用blit时,第一帧保留在画布中(Matplotlib animation: first frame remains in canvas when using blit)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
Matplotlib动画:使用blit时,第一帧保留在画布中(Matplotlib animation: first frame remains in canvas when using blit)

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

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

I'm trying to plot two rotating ellipses using the Matplotlib animation library, and I managed to get it working (more or less). The problem is that the first frame that is being rendered does not update, so while I got two rotating ellipses in my canvas, I also have the ellipses in their original position/orientation. Check out my simple piece of code:

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

Any idea how to fix this? I could of course turn off blit, but that makes it horribly slow, so that's not really an option.

EDIT: Final (working) Code

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

最满意答案

尝试这个:

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

显示正确行为的屏幕截图

试试这种方法(不是我只用一个椭圆来测试,它在这里也很好):

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

Try this:

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

Try this other approach (not I've used only one ellipse just for testing, it also renders fine here):

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

更多推荐

本文发布于:2023-04-20 16:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/af2b4ad94fc14c7b84dda4b1f73ff122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   动画   blit   Matplotlib   canvas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!