Python matplotlib.pyplot.FuncAnimation.save()无意中裁剪动画。我如何避免这种情况?

Python matplotlib.pyplot.FuncAnimation.save()无意中裁剪动画。我如何避免这种情况?,python,matplotlib,animation,crop,Python,Matplotlib,Animation,Crop,我想保存通过plt.FuncAnimation()制作的动画。它的侧面有一个长标题和文本框(显示各种参数)。我试过ffpmeg(.mp4)、枕头(.gif)和imagemagick(.gif)书写器。所有的尝试都会导致输出文件看起来在主图形上放大,从而切断文本并降低质量。我如何避免这种情况 通过plt.savefig()保存图形时也会出现同样的问题,但这是通过plt.tight\u layout()或plt.savefig(bbox\u inches='tight')解决的。这两种方法都不适用于

我想保存通过
plt.FuncAnimation()
制作的动画。它的侧面有一个长标题和文本框(显示各种参数)。我试过ffpmeg(.mp4)、枕头(.gif)和imagemagick(.gif)书写器。所有的尝试都会导致输出文件看起来在主图形上放大,从而切断文本并降低质量。我如何避免这种情况

通过
plt.savefig()
保存图形时也会出现同样的问题,但这是通过
plt.tight\u layout()
plt.savefig(bbox\u inches='tight')
解决的。这两种方法都不适用于plt.FuncAnimation().save()

下面是一个例子:

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""

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

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

ax.set_title('This is a really long title that is cropped in the .mp4, but not the .ipynb output.')

ax.text(3, 0.5, 'Text outside the graph is also cropped in the .mp4, but not the .ipynb output.')

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

我通过使用空的子图来解决这个问题。有关如何完全隐藏子地块的详细信息,请参见。然后,您可以根据需要在子批次中添加形状/文本。

我建议创建图形时,从一开始就不要有内容重叠。这样你就避免了整个问题。如果您确实需要依靠通过
bbox\u inches=“tight”
展开的图形,您可以在调用
anim.save
时通过
savefig\u kwargs
将其作为参数提供。非常感谢您的快速回复。使用
savefig_kwargs={'bbox_inches':'tight'}
我得到:“警告:丢弃'savefig_kwargs'中的'bbox_inches'参数,因为它可能会导致帧大小变化,这不适合动画。”事后看来,这是有道理的。请您解释一下,以一开始就没有内容重叠的方式创建图形是什么意思?你是在建议我使用子绘图吗?哦,是的,对不起,我忘了那个警告。我的意思是,如果标题是7英寸宽,请确保图形也是7英寸宽,这样它就不会重叠。@importanceofbeingerest是的,我同意。但是,动画仍然会切断文本注释。此外,如果标题或轴标签的大小较大,它们也会部分被切断。我尝试了
bbox\u extra\u美术师
savefig-kwarg*,但没有成功。*对确保不要将任何图元放置在地物边界之外。