Could it be changed such that figaspect takes a float
> as its argument and the 2 lines to compute arr_ratio
> are moved from figaspect to matshow before calling
> figaspect ? (+ change the figaspect doc string) It will
> make figaspect more general and allow us to create a
> figure with a given aspect ratio without
> copying/pasting of nearly all the figaspect machinery.
Good idea -- I overloaded figaspect to do both (it works with either
an array or a number argument). Code is below and in CVS
Checking in lib/matplotlib/figure.py;
/cvsroot/matplotlib/matplotlib/lib/matplotlib/figure.py,v <-- figure.py
new revision: 1.34; previous revision: 1.33
> 2/ Does anybody intend to add a Patch object to draw
> polygons with holes (I do not know how to do it) ?
Not presently, but this would be nice.
JDH
def figaspect(arg):
"""
Create a figure with specified aspect ratio. If arg is a number,
use that aspect ratio. If arg is an array, figaspect will
determine the width and height for a figure that would fit array
preserving aspcect ratio. The figure width, height in inches are
returned. Be sure to create an axes with equal with and height,
eg
Example usage:
# make a figure twice as tall as it is wide
w, h = figaspect(2.)
fig = Figure(figsize=(w,h))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.imshow(A, **kwargs)
# make a figure with the proper aspect for an array
A = rand(5,3)
w, h = figaspect(A)
fig = Figure(figsize=(w,h))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.imshow(A, **kwargs)
Thanks to Fernando Perez for this function
"""
isarray = hasattr(arg, 'shape')
# min/max sizes to respect when autoscaling. If John likes the idea, they
# could become rc parameters, for now they're hardwired.
figsize_min = array((4.0,2.0)) # min length for width/height
figsize_max = array((16.0,16.0)) # max length for width/height
#figsize_min = rcParams['figure.figsize_min']
#figsize_max = rcParams['figure.figsize_max']
# Extract the aspect ratio of the array
if isarray:
nr,nc = arg.shape[:2]
arr_ratio = float(nr)/nc
else:
arr_ratio = float(arg)
# Height of user figure defaults
fig_height = rcParams['figure.figsize'][1]
# New size for the figure, keeping the aspect ratio of the caller
newsize = array((fig_height/arr_ratio,fig_height))
# Sanity checks, don't drop either dimension below figsize_min
newsize /= min(1.0,*(newsize/figsize_min))
# Avoid humongous windows as well
newsize /= max(1.0,*(newsize/figsize_max))
# Finally, if we have a really funky aspect ratio, break it but respect
# the min/max dimensions (we don't want figures 10 feet tall!)
newsize = clip(newsize,figsize_min,figsize_max)
return newsize