matplotlib动画保存不遵循blit = True,但在plt.show()中似乎还可以 [英] matplotlib animation save is not obeying blit=True but it seems to work just fine in plt.show()

查看:119
本文介绍了matplotlib动画保存不遵循blit = True,但在plt.show()中似乎还可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,正在尝试使用matplotlib为文本设置动画.通过几个在线示例得出以下代码:

I'm quite new to Python and am trying to animate text using matplotlib. used several online examples to arrive at the following code:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

plt.xlabel('Distance')
plt.ylabel('Height')
plt.title('Object Trajectory \n')

plt.legend(loc="upper right", markerscale=4, fontsize=10)
plt.grid()

text=ax.text(3,1,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
text2=ax.text(0,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

def init():
    ax.set_xlim(0,10)
    ax.set_ylim(0,10)
    return text,text2

def update(frame):        
    #Moving a text
    text=ax.text(3,1+(int(frame))/30,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
    text2=ax.text(0+(int(frame))/30,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

    return text,text2

anim = animation.FuncAnimation(fig, update, init_func=init, frames=120, interval=10, blit=True)

anim.save('try_animation.mp4',dpi=160,fps=30, writer="ffmpeg")

plt.show()

因此,当我在控制台中运行它时,我可以看到文本很好地移动.但是,当我将其保存到MP4文件中时,文本似乎不会变白.请帮忙.

So when I run it in console, I can see the texts moving nicely. But when I save it to a MP4 file the text doesn't seem to blit. Please Help.

谢谢

这是保存的视频文件的屏幕截图

推荐答案

您观察到的是预期的行为.划线是一种仅刷新部分图形输出的技术.在matplotlib情况下,不是刷新整个图,而是仅刷新其中的一部分(即轴内的区域),并且仅绘制由动画功能返回的艺术家.这样可以使屏幕上的动画速度更快.

What you observe is the expected behaviour. Blitting is a technique used to refresh only part of a graphics output. In the matplotlib case, instead of drawing the complete figure, only part of it, namely the region inside the axes, is refreshed and only those artists returned by the animating function are drawn. This allows to have a faster animation speed on screen.

但是,保存动画时,需要完整绘制每个帧.

因此,为了移动文本,应该更新一个文本的位置,而不是一遍又一遍地创建新文本.可以使用

So in order to have a text moving, one should rather update a single text's position, instead of creating new texts over and over again. This can be done with

text.set_position((x,y))

该示例因此看起来像

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

plt.xlabel('Distance')
plt.ylabel('Height')
plt.title('Object Trajectory \n')
plt.grid()

text=ax.text(3,1,'Moving Text', ha="left", va="bottom",clip_on=True,rotation=90,fontsize=15)    
text2=ax.text(0,1,'Moving Text', ha="left", va="bottom",clip_on=True,fontsize=15)    

def init():
    ax.set_xlim(0,10)
    ax.set_ylim(0,10)
    return text,text2

def update(frame):        
    #Moving a text
    text.set_position((3, 1+(int(frame))/30))
    text2.set_position((0+(int(frame))/30,1))
    return text,text2

anim = animation.FuncAnimation(fig, update, init_func=init, frames=120, interval=10, blit=True)

anim.save('try_animation.mp4',dpi=160,fps=30, writer="ffmpeg")

plt.show()

这篇关于matplotlib动画保存不遵循blit = True,但在plt.show()中似乎还可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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