matplotlib savefig 裁剪最后一行和最后一列

matplotlib savefig cropping last row and column

我正在尝试绘制正在遍历的迷宫,当我使用 plt.show() 时,我得到了整个迷宫,而当我使用 savefig 保存同一图像时,最后一行和正在绘制的列得到 cropped/cut关闭。

这是用于绘制迷宫的代码。 (附上迷宫截图。)

def maze_plot_final(maze):
    fig, ax = plt.subplots()
    ax.cla()
    cmap = plt.cm.get_cmap()
    cmap.set_bad("white")
    ax.imshow(maze, cmap=cmap)
    plt.savefig('figs/dfs-new.png', dpi=1000, bbox_inches='tight',)

The entire maze traversed

This is the screenshot of the image generated by savefig

This is the screenshot of the last part of maze shown by plt.show

您是否尝试过在保存前设置轴限制?

plt.xlim(tuple_x)
plt.ylim(tuple_y)

其中 tuple_x:

(x0,xf) := interval of interest