首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从动画matplotlib GIF中删除空白?

如何从动画matplotlib GIF中删除空白?
EN

Stack Overflow用户
提问于 2022-08-30 14:11:30
回答 2查看 61关注 0票数 0

我试图保存使用matplotlib创建的GIF,但是周围有太多的空白。我尝试将bbox_inches = "tight"作为参数传递,但得到了以下警告:

代码语言:javascript
运行
复制
Warning: discarding the 'bbox_inches' argument in 'savefig_kwargs' as it may cause frame size to vary, which is inappropriate for animation.

下面是我用来生成GIF的MWE脚本:

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation

fig, ax = plt.subplots()

imgs = []
for idx in range(10):
    img = np.random.rand(10, 10)
    img = ax.imshow(img, animated=True)
    imgs.append([img])

anim = ArtistAnimation(fig, imgs, interval=50, blit=True, repeat_delay=1000)
anim.save("test.gif")

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-30 15:29:09

您需要首先设置图形的大小(例如:fig.set_size_inches(5,5)),然后关闭轴/网格/etc (ax.set_axis_off()),然后调整图形的子图参数(fig.subplots_adjust(left=0, bottom=0, right=1, top=1))。有关更多信息,请查看this question的答案。

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation

fig, ax = plt.subplots()
fig.set_size_inches(5,5)
ax.set_axis_off() # You don't actually need this line as the saved figure will not include the labels, ticks, etc, but I like to include it
fig.subplots_adjust(left=0, bottom=0, right=1, top=1)
imgs = []
for idx in range(10):
    img = np.random.rand(10, 10)
    img = ax.imshow(img, animated=True)
    imgs.append([img])
anim = ArtistAnimation(fig, imgs, interval=50, blit=True, repeat_delay=1000)
anim.save("test.gif")

无边界的Gif:

票数 1
EN

Stack Overflow用户

发布于 2022-08-30 14:25:48

在创建图形之后,在图形上使用subplot_adjust

代码语言:javascript
运行
复制
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73543900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档