I am trying to animate a set of plot but for some reason the methods for removing whitespace from plot borders in images generated by imshow()
are not working. Here are some examples and the images that result:
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)
This clearly still has borders, axes, whitespace etc. I can remove either the tight_layout()
or the subplots_adjust()
and it doesn't change the behavior significantly. I did some digging and found several answers here that suggested the following:
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')
It might not be obvious from this page but if you look at how the image here aligns with the left boundary of the code box above it you can see that there is still white space. Trying to use tight_layout()
or subplots_adjust()
in this case doesn't help either. It seems like it must be possible to totally remove the white boundary but it's not clear to me what else to try.
Edit: The following fix was suggested in the comments, deriving from a different post:
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)
where I am using the purple background to indicate where the figure ends. This leads to the following output:
This also did not work.
It might also be useful to mention that I am using the Spyder IDE, which may have some additional idiosyncrasies that I am unaware of.