wdg
wdg

Reputation: 1797

Adjust figure margin

The following code gives me a plot with significant margins above and below the figure. I don't know how to eliminate the noticeable margins. subplots_adjust does not work as expected.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
ax.set_aspect('equal')
plt.tight_layout()

tight_layout eliminates some of the margin, but not all of the margins.

What I wanted is actually setting the aspect ratio to any customized value and eliminating the white space at the same time.

Update: as Pierre H. puts it, the key is to change the size of the figure container. So my question is: Could you suggest a way to accommodate the size of the figure to the size of the axes with arbitrary aspect ratio?

In other words, first I create a figure and an axes on it, and then I change the size of the axes (by changing aspect ratio for example), which in general will leave a portion of the figure container empty. At this stage, we need to change the size of the figure accordingly to eliminate the blank space on the figure container.

Upvotes: 53

Views: 139422

Answers (7)

YASUR
YASUR

Reputation: 21

You can use like:

plt.subplots_adjust(wspace=1,hspace=0.5,left=0.1,top=0.9,right=0.9,bottom=0.1)

And delete the item bbox_inches='tight' in plt.savefig().

Upvotes: 2

beahacker
beahacker

Reputation: 1770

I think what you need is, and it works well for me.

plt.axis('tight')

This command will automatically scale the axis to fit tightly to the data. Also check the answer of Nuno Aniceto for a customized axis. The documents are in https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.axis.

Be aware that

plt.savefig(filename, bbox_inches='tight')

will help remove white space of all the figure including labels, etc, but not the white space inside the axes.

Upvotes: 9

mason
mason

Reputation: 261

You should use add_axes if you want exact control of the figure layout. eg.

left = 0.05
bottom = 0.05 
width = 0.9
height = 0.9
ax = fig.add_axes([left, bottom, width, height])

Upvotes: 7

Minh
Minh

Reputation: 1001

I just discovered how to eliminate all margins from my figures. I didn't use tight_layout(), instead I used:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20,20))
ax = plt.subplot(111,aspect = 'equal')
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

Hope this helps.

Upvotes: 90

Nuno Aniceto
Nuno Aniceto

Reputation: 1982

After plotting your chart you can easily manipulate margins this way:

plot_margin = 0.25

x0, x1, y0, y1 = plt.axis()
plt.axis((x0 - plot_margin,
          x1 + plot_margin,
          y0 - plot_margin,
          y1 + plot_margin))

This example could be changed to the aspect ratio you want or change the margins as you really want. In other stacktoverflow posts many questions related to margins could make use of this simpler approach.

Best regards.

Upvotes: 31

kevin
kevin

Reputation: 943

tight_layout(pad=0) will meet your need. http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.tight_layout

Upvotes: 22

Pierre H.
Pierre H.

Reputation: 433

I think the subplot_adjust call is irrelevant here since the adjustment is overridden by tight_layout. Anyway, this only change the size of the axes inside the figure.

As tcaswell pointed it, you need to change the size of the figure. Either at creation (my proposition below) or after, using fig.set_size_inches. I'm here creating a figure with a 1:1 aspect ratio using the figsize=(6,6) argument (of course 6 inches is an arbitrary choice):

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
ax.set_aspect('equal')
plt.tight_layout()

Upvotes: 6

Related Questions