2

I've seen many posts and answers online trying to answer this question.
However using bbox_inches = 'tight' the legend disappears.

This is one of my figures: enter image description here

Since I have the legend outside the plot frame, I would like to remove only the top and bottom white space.

Anyone knows how to remove at least the top white space?

Thanks a lot!

psoares
  • 4,733
  • 7
  • 41
  • 55

2 Answers2

4

Have you tried using subplots_adjust()? See, for example, the answer of @DaveP to this question: Reduce left and right margins in matplotlib plot

Also, look at the answer by @Tian Chu to the same question.

EDIT: This works for me:

import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2,3],[5,6,7],'gs-')
plt.subplots_adjust(top=0.99, right=0.99)
plt.show()
Community
  • 1
  • 1
ev-br
  • 24,968
  • 9
  • 65
  • 78
  • Yes, I've seen this answer but perhaps I've used wrong values (they were smaller than those provide in the answer) because half of my plot dissapeared. I'll try again and let you know – psoares May 10 '12 at 10:46
  • 1
    @pavid: there's a possible gotcha mentioned in the comment by @drootang: the arguments of `subplots_adjust` are the values assigned to parameters: to have the top margin of `5%` of the figure size use `top=0.95` etc – ev-br May 10 '12 at 11:01
  • Then join me in upvoting that answer and that comment :-) – ev-br May 10 '12 at 11:07
  • Yes, I already vote in your comment :) I will accept the answer as soon as I try it! :) – psoares May 10 '12 at 11:10
  • I don't really understand how this work. I've tried `fig.subplots_adjust(top=0.9)` but it seems the white space at top is even increasing, at least not decreasing.. – psoares May 10 '12 at 12:48
  • Can you try (a trivial) snipped from the edited post? It works as expected on my system, does it work on yours? – ev-br May 10 '12 at 13:00
  • it works! Though I don't really understand this `ax.plot([1,2,3],[5,6,7],'gs-')` but gives me the correct output – psoares May 10 '12 at 15:15
  • Glad that it worked. As far as `ax.plot` is concerned, see here: http://matplotlib.sourceforge.net/faq/usage_faq.html – ev-br May 10 '12 at 15:25
1

I usually don't use the bbox_inches = 'tight' feature, since it doesn't work very reliably, as you already found out. I'd rather produce a PDF with bounds and then crop them using external tools. To do this seamless from python, I use

os.system('pdfcrop %s %s &> /dev/null &'%(pdf_in, pdf_out))

Here, pdf_in is the PDF you produced from matplotlib and pdf_out will be your final result.

David Zwicker
  • 23,581
  • 6
  • 62
  • 77
  • Do you know if it is possible to achieve this with an image? I'm trying to produce automatic images.. – psoares May 10 '12 at 10:13
  • What do you mean by 'image'? A pixel based format? The strategy of course works with any format as long as you have an external program doing the cropping. www.imagemagick.org might help with that... – David Zwicker May 10 '12 at 10:58
  • @pavid In addition to pdfcrop for pdf's, cropping a PNG is as simple as convert -trim FOO.png using imagemagick. This can be worked in an "automatic" way using a os.system call if you like. – Hooked May 11 '12 at 13:44