schwarz
schwarz

Reputation: 521

Remove white margins in a running figure

I am specifically asking for doing this in a running figure since the solution of plt.savefig('blaa.png', bbox_inches='tight') is of no concern to me. I do not want to save the figure, I want to keep it running without any margins. This is how my figure is created:

mpl.rcParams['toolbar'] = 'None'

fig = plt.figure()
ax = fig.add_subplot(111)
img = plt.imread('blaaa.jpg')
ax.imshow(img, extent=[-180, 180, -90, 90], aspect=1)

ax.set_axis_off()
plt.tight_layout(pad=0, h_pad=0, w_pad=0, rect=None)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

After doing this, I still get white margins on the left side. Any ideas?

Upvotes: 0

Views: 136

Answers (2)

DavidG
DavidG

Reputation: 25380

As an alternative to the other answer, you can use different keywords in your call to subplots_adjust:

Note that wspace and hspace are the space between subplots so do not do anything here, you want left= and right=.

mpl.rcParams['toolbar'] = 'None'

fig = plt.figure()
ax = fig.add_subplot(111)
img = plt.imread('blaaa.jpg')
ax.imshow(img, extent=[-180, 180, -90, 90], aspect=1)

ax.set_axis_off()
plt.subplots_adjust(left=0, right=1)
plt.show()

If you wanted your image to span the entire figure you could use aspect="auto" in imshow() which may not be applicable in this case if you want a certain aspect ratio

Upvotes: 1

JE_Muc
JE_Muc

Reputation: 5784

You need to set the axes position to the full bounding box:

mpl.rcParams['toolbar'] = 'None'

fig = plt.figure()
ax = fig.add_subplot(111)
img = plt.imread('blaaa.jpg')
ax.imshow(img, extent=[-180, 180, -90, 90], aspect=1)

ax.set_axis_off()
plt.tight_layout(pad=0, h_pad=0, w_pad=0, rect=None)
plt.subplots_adjust(wspace=0, hspace=0)  # this shouldn't be needed afaik
ax.set_position([0, 0, 1, 1])
plt.show()

Upvotes: 1

Related Questions