1

Ok, I have exhausted all my google searches and have reached a point of I have no idea how to do that. It seems so simple, still, i have tried like 20 approaches and nothing works. I need help...

Using the following code i get this:

import matplotlib.pyplot as plt

plt.rcParams['axes.facecolor'] = "#F3F0E0"
plt.rcParams['figure.facecolor'] = "#F3F0E0"

plt.figure(figsize = (6,3))

plt.plot([1,3,2])

plt.show()

Image that i get

I would like to have something like this, with ample margins left and right. Would be also nice to have a simmilar aproach for top and bottom.

Image that i want

I have played around with tight_layout() and many other settings but I'm failing miserably to make it work.

An ugly hack would be to include:

plt.text(1.1, 1.1, "text", fontsize=14, transform=plt.gcf().transFigure)
plt.text(-.1, -.1, "text", fontsize=14, transform=plt.gcf().transFigure)

And then to make the text the same color as the background But of course i would like to have a more elegant way to do it

The idea is that a plot with some margins left and right looks more relaxed to the eye. Of course, I can fix this in other software but I would like to have it ready in plt.

Example image

  • Did you notice `plt.tight_layout` has parameters to experiment with? `pad=..., h_pad=..., w_pad=...`? [docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html). Note that the larger you make the margins, the less space the plot itself gets. It all depends. – JohanC Jan 02 '22 at 19:06
  • Thanks for your answer. I did experiment with that. But 'pad' makes my plot smaller by the factor I include, and w/h pad is not really doing anything. I think this is because it is a plot with a single plot. I have tried with multiple subplots and padding does work in that case. For what i want to achieve in this example, a smaller plotting area is ok for me. – Is Mael Cabral Jan 02 '22 at 19:47
  • Did you try subplots_adjust? https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots_adjust.html – Jody Klymak Jan 02 '22 at 20:09
  • See [How to disable bbox_inches='tight' when working with matplotlib inline in ipython notebook](https://stackoverflow.com/questions/26714626/how-to-disable-bbox-inches-tight-when-working-with-matplotlib-inline-in-ipytho) – JohanC Jan 02 '22 at 20:42

1 Answers1

0

You can specify the left and right margin in the gridspec:

import matplotlib.pyplot as plt

plt.rcParams['axes.facecolor'] = "#F3F0E0"
plt.rcParams['figure.facecolor'] = "#F3F0E0"

fig, ax = plt.subplots(figsize = (6,3), gridspec_kw={'left': .2, 'right': .8})

ax.plot([1,3,2])
plt.show() 

enter image description here

If you want to apply tight_layout, you can simply specify the box where to put the plot:

import matplotlib.pyplot as plt

plt.rcParams['axes.facecolor'] = "#F3F0E0"
plt.rcParams['figure.facecolor'] = "#F3F0E0"

plt.figure(figsize = (6,3))

plt.plot([1,3,2])

plt.tight_layout(rect=(.2, 0, .8, 1))

plt.show()

enter image description here


If you're using the IPyhton inline backend, add the following line at the beginning of your notebook to see the effect:
%config InlineBackend.print_figure_kwargs = {'bbox_inches': None}
Stef
  • 28,728
  • 2
  • 24
  • 52
  • Do i have a setting wrong? because i get this: https://i.stack.imgur.com/SnjRJ.png – Is Mael Cabral Jan 02 '22 at 20:37
  • @IsMaelCabral It seems you forgot to mention you are running this in a Jupyter notebook. Jupyter automatically removes the superfluous padding to better show the main plot. – JohanC Jan 02 '22 at 20:39
  • Ah, I didn't assume you're working with IPython because of the `plt.show()` commands. IPython inline by default uses `tight` without parameters. Let me think a bit... – Stef Jan 02 '22 at 20:40
  • So that's the reason! ... I was not aware of the different behavior on Notebooks. Is it any way I can replicate this solution on a notebook? – Is Mael Cabral Jan 02 '22 at 20:42
  • just use for instance `%matplotlib qt` at the beginning of your notebook; to have the uncropped figure in the notebook I'll have to think a bit... – Stef Jan 02 '22 at 20:46
  • see my update about configuring the inline backend to see the uncropped figure in the notebook – Stef Jan 02 '22 at 20:54