How to put more whitespace around my plots?

2024/10/14 9:21:07

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?

Answer

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)/2ax1 = 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.

https://en.xdnf.cn/q/69428.html

Related Q&A

using ols from statsmodels.formula.api - how to remove constant term?

Im following this first example in statsmodels tutorial:http://statsmodels.sourceforge.net/devel/How do I specify not to use constant term for linear fit in ols?# Fit regression model (using the natur…

Is numerical encoding necessary for the target variable in classification?

I am using sklearn for text classification, all my features are numerical but my target variable labels are in text. I can understand the rationale behind encoding features to numerics but dont think t…

django - regex for optional url parameters

I have a view in django that can accept a number of different filter parameters, but they are all optional. If I have 6 optional filters, do I really have to write urls for every combination of the 6 …

How do I remove transparency from a histogram created using Seaborn in python?

Im creating histograms using seaborn in python and want to customize the colors. The default settings create transparent histograms, and I would like mine to be solid. How do I remove the transparency?…

Set confidence levels in seaborn kdeplot

Im completely new to seaborn, so apologies if this is a simple question, but I cannot find anywhere in the documentation a description of how the levels plotted by n_levels are controlled in kdeplot. T…

OpenCV (cv2 in Python) VideoCapture not releasing camera after deletion

I am relatively new to Python, just having learnt it over the past month or so and have hacked this together based off examples and others code I found online.I have gotten a Tkinter GUI to display the…

Paho MQTT Python Client: No exceptions thrown, just stops

I try to setup a mqtt client in python3. This is not the first time im doing this, however i came across a rather odd behaviour. When trying to call a function, which contains a bug, from one of the c…

SSH Key-Forwarding using python paramiko

We currently run a script on our desktop that uses paramiko to ssh to a remote linux host. Once we are on the remote linux host we execute another command to log into another remote machine. What we wa…

Is it possible to ignore Matplotlib first default color for plotting?

Matplotlib plots each column of my matrix a with 4 columns by blue, yellow, green, red.Then, I plot only the second, third, fourth columns from matrix a[:,1:4]. Is it possible to make Matplotlib ignore…

`sock.recv()` returns empty string when connection is dead on non-blocking socket

I have a non-blocking socket in Python called sock. According to my understanding the recv() method should raise an exception if the connection has been closed by the peer, but it returns an empty stri…