Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put more whitespace around my plots?

I have a figure that contains two subplots in two rows and one column like so:

fig, (ax1, ax2) = subplots(
    nrows=2,
    ncols=1,
)

The two subplots are pie charts, therefore I want their axes to be square. After browsing the API, the following should be the correct way to adjust the axes of each subplot:

ax1.axis([0, 8, 0, 8])
ax2.axis([0, 8, 0, 8])

Then I adjust the size of the entire figure. Since the width of each subplot is 8 inches, I set the width of the figure to 8 inches as well and the height of the figure to 2*8=16 inches:

fig.set_size_inches(8, 16)

These settings result in nicely formatted pie charts. However, a label for one of the wedges of a pie is cut off at the right edge of the figure when I save the figure to a PDF file. The figure is more narrow in the saved file than in the preview window. That's why I want to widen the figure along the x axis but by retaining the axes of the subplots. If I just try to widen the figure by doing:

fig.set_size_inches(12, 16)

The figure gets wider now but, unfortunately, the two pie charts as well. From my understanding, the axis() calls should retain the dimensions of the subplots but they don't. After reading and trying out a lot about autoscale, xlim, ylim etc., I'm even more confused and nothing works.

I simply want to widen the figure by retaining the dimensions of the subplots so that there is more whitespace along the x axis and the labels can be displayed without cutting them off. How do I do that?

like image 920
pemistahl Avatar asked Feb 17 '23 21:02

pemistahl


1 Answers

I would suggest using fig.tight_layout. You have roughly two paths, either tweak pad

fig.tight_layout(pad=2)

which while move all of your edges in from the edge of the figure or

fig.tight_layout(rect=[0,0,.8,1]) 

which will adjust the size of the box (in units of fraction of the figure) that the sub-plots are squeezed into. This only works in newer (1.2 or later) versions.

You can also create you sub-plots by hand (which is super annoying, but gives you complete control) or use gridspec. Layout out sub-plots seems to be a problem matplotlib has a large number of ways to solve.

The annoying way do to this is

fig = plt.figure()


top_offset = .07
left_offset = .15
right_offset = .2
bottom_offset = .13
hgap = .1
ax_width = 1-left_offset - right_offset
ax_height = (1-top_offset - bottom_offset - hgap)/2

ax1 = fig.add_axes([left_offset, bottom_offset + ax_height + hgap, ax_width, ax_height])
ax2 = fig.add_axes([left_offset, bottom_offset, ax_width, ax_height])

with the 5 parameters tweaked to be what ever makes your figures look good.

like image 125
tacaswell Avatar answered Feb 20 '23 17:02

tacaswell