0

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)

Test1

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')

enter image description here

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:

enter image description here

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.

algol
  • 399
  • 3
  • 15
  • Did you read [this](https://stackoverflow.com/questions/11837979/removing-white-space-around-a-saved-image-in-matplotlib)? – Sheldore Feb 13 '20 at 22:47
  • Yes but I am trying not to save the images as I produce the animation, because doing so slows down the animation process by quite a bit. – algol Feb 13 '20 at 22:55
  • The linked post seems to work fine, also without saving the image. Just using `plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0); plt.margins(0, 0)`. Try to set `plt.subplots(..., facecolor='purple')` to clearly show where there still would be a part of the background visible. – JohanC Feb 13 '20 at 23:46
  • Be careful not to use `None` instead of `0`. None usually means to take the default values. Zero means zero. – JohanC Feb 13 '20 at 23:50
  • I have updated the post with an attempt at using that fix. – algol Feb 14 '20 at 18:31
  • 1
    What you show is the output png inlined in an IPython environment. However, since it seems you want to animate the result, this would be irrelevant. `ani.save()` would give you the correct output. – ImportanceOfBeingErnest Feb 14 '20 at 19:05
  • Were you able to solve this? I am stuck with the same problem. – MollieVX Sep 12 '21 at 13:04
  • Sadly I believe that solution that I ended up with was just not to use Spyder – algol Sep 30 '21 at 03:37
  • 1
    in Spyder (5.1.5) if you go to `Preferences >> IPython console >> Graphics` and look in `Inline backend` section untick the option for `Use a tight layout for inline plots`. Then `fig.subplots_adjust(0,0,1,1,0,0)` works beautifully! – Kardo Paska Oct 29 '21 at 19:00

0 Answers0