我试图保存使用matplotlib创建的GIF,但是周围有太多的空白。我尝试将bbox_inches = "tight"
作为参数传递,但得到了以下警告:
Warning: discarding the 'bbox_inches' argument in 'savefig_kwargs' as it may cause frame size to vary, which is inappropriate for animation.
下面是我用来生成GIF的MWE脚本:
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")
发布于 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的答案。
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:
发布于 2022-08-30 14:25:48
在创建图形之后,在图形上使用subplot_adjust
。
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
https://stackoverflow.com/questions/73543900
复制相似问题