pythonpython-3.xmatplotlibimshow

Matplotlib savefig cropping last row and column


I'm trying to plot the maze being traversed, when I do use plt.show() I get the entire maze whereas when I save the same image using savefig the last row and column being plotted gets cropped/cut off.

This is the code used for plotting the maze (I have attached the screenshots of the maze):

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


Solution

  • Have you tried setting the axis limits before saving?

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

    where tuple_x:

    (x0,xf) := interval of interest