首页 > 解决方案 > 从 matplotlib imshow 的边框中删除所有空格

问题描述

我正在尝试为一组绘图设置动画,但由于某种原因,从生成的图像中的绘图边框中删除空格的方法imshow()不起作用。以下是一些示例和生成的图像:

figsize=(10,10)
fig, ax = plt.subplots(figsize=figsize)
fig.tight_layout()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
img = ax.imshow(np.random.normal(0,2,(200,200)), animated = True)

测试1

这显然仍然有边界、轴、空白等。我可以删除tight_layout()orsubplots_adjust()并且它不会显着改变行为。我做了一些挖掘,在这里找到了几个答案,建议如下:

figsize=(4,4)
fig, ax = plt.subplots(figsize=figsize)

ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
img = ax.imshow(np.random.normal(0,2,(200,200)), animated = True,aspect = 'auto')

在此处输入图像描述

从这个页面上看可能不是很明显,但如果你看看这里的图像如何与上面代码框的左边界对齐,你会发现仍然有空白。尝试使用tight_layout()orsubplots_adjust()在这种情况下也无济于事。似乎必须有可能完全去除白色边界,但我不清楚还有什么可以尝试的。

编辑:评论中建议了以下修复,来自不同的帖子

fig, ax = plt.subplots(facecolor='purple')
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
ax.margins(0,0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
img = ax.imshow(np.random.normal(0,2,(200,200)), animated = True)

我使用紫色背景来指示图形结束的位置。这导致以下输出:

在此处输入图像描述

这也没有奏效。

提到我正在使用 Spyder IDE 也可能很有用,它可能有一些我不知道的额外特质。

标签: pythonmatplotlibanimation

解决方案


推荐阅读