1

I am using the following example Example to create two polar contour subplots. When I create as the pdf there is a lot of white space which I want to remove by changing figsize.

I know how to change figsize usually but I am having difficulty seeing where to put it in this code example. Any guidance or hint would be greatly appreciated.

Many thanks!

import numpy as np
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)

r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)

plt.show()
Community
  • 1
  • 1
James
  • 13
  • 1
  • 3

2 Answers2

2

You can easily just put plt.figsize(x,y) at the beginning of the code, and it will work. plt.figsize changes the size of all future plots, not just the current plot.

However, I think your problem is not what you think it is. There tends to be quite a bit of whitespace in generated PDFs unless you change options around. I usually use

plt.savefig( 'name.pdf', bbox_inches='tight', pad_inches=0 )

This gives as little whitespace as possible. bbox_inches='tight' tries to make the bounding box as small as possible, while pad_inches sets how many inches of whitespace there should be padding it. In my case I have no extra padding at all, as I add padding in whatever I'm using the figure for.

cge
  • 9,552
  • 3
  • 32
  • 51
  • Thanks, I wasn't aware of the comments in the savefig - this helps. Actually using plt.figsize=(5,5), say, at the beginning of the code has no effect on my output. – James Mar 10 '13 at 10:10
  • If you're doing plt.figsize=(5,5) that won't work; it needs to be plt.figsize(5,5) without the equals. There is a function figsize, which sets the default figure size, and a named argument figsize to figure and subplots which sets the figsize for that particular figure. – cge Mar 11 '13 at 00:53
1

Another way to do this would be to use the figsize kwarg in your call to plt.subplots.

fig, ax = plt.subplots(figsize=(6,6), subplot_kw=dict(projection='polar')).

Those values are in inches, by the way.

Paul H
  • 65,268
  • 20
  • 159
  • 136