Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting space around figure with subplots

I'm using Matplotlib and struggling to get exactly the right amount of whitespace around a figure I'm plotting that contains a grid of subplots.

Requirements: be able to choose the aspect ratio of all the subplots (they're all the same), define exactly how much whitespace there is between the subplots and the top, bottom and left of the (saved) figure edge. I don't need any extra space on the right.

The code I have so far contains the following:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(ncols=6, nrows=2, sharex=True, 
                         sharey=True, figsize=(16,8))

axes[0,0].plot(x,y)
etc...

plt.subplots_adjust(hspace=1.0, wspace=0.02, bottom=0.17, left=0.075, top=0.18)

plt.savefig('Fig1_matplot.pdf', bbox_inches='tight')

As it is, if I don't specify figsize=() the aspect ratio of the subplots is wrong. If I do, then the aspect ratio changes whenever I change parameters in subplots_adjust(). Apparently left cannot be >= right and bottom cannot be >= top. If I do define top to be anything other than 0 it completely compresses all of the subplots down to a line at the bottom of the figure.

If I specify bbox_inches='tight' then it cuts off my defined whitespace at the bottom and left, but if I don't specify it then I'm left with excess whitespace on the right since I can't get rid of it (right would be <= left).

I'm a bit stuck, any help would be much appreciated.

Cheers.

like image 597
thosphor Avatar asked Jan 29 '15 17:01

thosphor


People also ask

How do you adjust subplot spacing?

You can use plt. subplots_adjust to change the spacing between the subplots. Show activity on this post. Using subplots_adjust(hspace=0) or a very small number ( hspace=0.001 ) will completely remove the whitespace between the subplots, whereas hspace=None does not.

How does subplot change figure size?

To change figure size of more subplots you can use plt. subplots(2,2,figsize=(10,10)) when creating subplots.

What does the PLT subplots_adjust () method uses to move around subplots on the grid?

subplots_adjust() function changes the space between subplots. By using parameters left, right, top, bottom, wspace, hspace, we can adjust the subplot's position.

How do you make subplots not overlap?

Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout() function.

How to change the spacing between subplots in a Python plot?

We can use plt.subplots_adjust () method to change the spacing between the subplots. wspace and hspace specify the space reserved between subplot. They are the fractions of axis width and height respectively. left, right, top and bottom parameters specify the positions of four sides of the subplots.

How do you adjust the left side of a subplot?

Syntax: subplots_adjust (self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) left : This parameter is the left side of the subplots of the figure.

What are the dimensions of the subplots of the figure?

They are the fractions of axis width and height, respectively. left, right, top and bottom parameters specify four sides of the subplots’ positions. They are the fractions of the width and height of the figure.

How to change the size of the subplots of a graph?

We can easily change the size of the subplots by changing the values in the width_ratios argument:


1 Answers

When using subplots_adjust, the values of left, right, bottom and top are to be provided as fractions of the figure width and height. In additions, all values are measured from the left and bottom edges of the figure. This is why right and top can't be lower than left and bottom. A typical set-up is:

plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)

On the other hand, if you want to provide white space as absolute values in inches, you will have to divide by the figure width and height. For instance in your case, a 1 inch margin around all edges can be achieved with:

plt.subplots_adjust(left=1/16.0, right=1-1/16.0, bottom=1/8.0, top=1-1/8.0)

For more versatility I usually parametrize the width and height of the figure as:

figw, figh = 16.0, 8.0
fig, axes = plt.subplots(ncols=6, nrows=2, sharex=True, sharey=True,
                         figsize=(figw, figh))
plt.subplots_adjust(left=1/figw, right=1-1/figw, bottom=1/figh, top=1-1/figh)

The hspace and wspace are counted as fractions of one panel's axes width and height, so that their absolute value depends on the figure dimensions, the outer margins and the number of subplots, which will require a tiny bit more math for fine-tuning.

like image 87
juseg Avatar answered Oct 17 '22 06:10

juseg