0

I am trying to add an arbitrarily big white margin (or padding) to a figure with subplots because I would like the subtitle of the figure not to overlap with any of the subplots or titles of these subplots. I am using Matplotlib 3.1.2.

Currently, I have the following source code.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(2, 1, figsize=(15, 10))

n = 10
x = np.arange(0, n)
y = np.random.rand(n)

ax[0].plot(x, y)
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')

y = np.random.rand(n)

ax[1].plot(x, y)
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')

fig.suptitle("I want to have white space around me!!!")
# fig.tight_layout(rect=[0, 0.03, 1, 0.80])
plt.subplots_adjust(top=0.85)

plt.show()

If I try to use either tight_layout or subplots_adjust (as suggested in several answers to this question Matplotlib tight_layout() doesn't take into account figure suptitle), it doesn't seem to have any effect on the margins. Here's the result of the execution of the previous example.

enter image description here

Is there a way to add an arbitrarily big white margin to the left, right, bottom and (or) top of a figure (with subplots)? I would like to specify the figure size and arbitrarily increase or decrease the white space around an image. I also would like the solution to work in case I decide to add a title for each of the subplots. How can this be done?

nbro
  • 15,395
  • 32
  • 113
  • 196
  • I guess you're working in a jupyter notebook? If so, the figure you would save with `fig.savefig("output.png")` would be correct, right?! And you would get the desired figure via `%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}` in an empty cell before your code. Or you can use the `%matplotlib notebook` backend. – ImportanceOfBeingErnest Dec 02 '19 at 20:39
  • @ImportanceOfBeingErnest No, I am not working with Jupyter notebooks. I am working with PyCharm in a virtual environment. – nbro Dec 03 '19 at 04:23
  • Ok, can you still try outside of pycharm (but still inside the virtual environment)? Would `fig.savefig("output.png")` give the correct output, or not? Does using a normal backend like `import matplotlib; matplotlib.use("TkAgg")` make it work? – ImportanceOfBeingErnest Dec 03 '19 at 04:44
  • @ImportanceOfBeingErnest Outside of PyCharm, the example above adds white space around the supertitle, even without using `import matplotlib; matplotlib.use("TkAgg")`. However, I have another (quite different) example, where, even if I execute it outside of PyCharm (i.e. from the terminal), it does not add the white space. – nbro Dec 11 '19 at 15:25
  • The use of the TkAgg backend was meant to see if it can change anything within PyCharm. If you want to share a [mcve] of the other quite different example, I can see why it doesn't work outside of PyCharm. (PyCharm is closed source, so it's generally hard to know why something doesn't work) – ImportanceOfBeingErnest Dec 11 '19 at 16:42
  • @ImportanceOfBeingErnest It's so annoying! Sometimes it seems to work, sometimes it doesn't. Now, I can't even plot an image, even though I am calling `show`. – nbro Dec 11 '19 at 16:47

1 Answers1

0
fig, axs = plt.subplots(2,1, figsize=(5,5))
fig.patch.set_facecolor('grey')
fig.suptitle("Look at all that grey space around me!!!")
fig.subplots_adjust(top=0.6, bottom=0.4, left=0.4, right=0.6)

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75