8000 ENH: fig.set_size to allow non-inches units · matplotlib/matplotlib@2f9b45d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f9b45d

Browse files
committed
ENH: fig.set_size to allow non-inches units
1 parent b96fb6f commit 2f9b45d

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,3 +2075,21 @@ def _check_and_log_subprocess(command, logger, **kwargs):
20752075
.format(command, exc.output.decode('utf-8')))
20762076
logger.debug(report)
20772077
return report
2078+
2079+
2080+
def _print_unit(st):
2081+
"""
2082+
Convert from a string with a number and units into inches
2083+
"""
2084+
print_units = {'cm': 2.54, 'pt': 72.0, 'mm': 25.4, 'in': 1.0}
2085+
try:
2086+
num = float(st[:-2])
2087+
except:
2088+
# let the parent handle the errors
2089+
return st
2090+
unit = st[-2:]
2091+
if unit in print_units:
2092+
return num / print_units[unit]
2093+
else:
2094+
# let the parent handle the errors
2095+
return st

lib/matplotlib/figure.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,19 +862,63 @@ def figimage(self, X, xo=0, yo=0, alpha=None, norm=None, cmap=None,
862862
self.stale = True
863863
return im
864864

865+
def set_size(self, w, h=None, forward=True):
866+
"""
867+
Set the figure size
868+
869+
Parameters
870+
----------
871+
872+
w: float, string, or two-tuple of either.
873+
874+
If a two-tuple, (width, height) of the figure.
875+
876+
If a float, width of figure in inches.
877+
878+
If a string, its a float followed by a 2-character suffix
879+
of either 'in', 'cm', 'mm', 'pt' (inches, centimeters,
880+
millimeters, or points). i.e. ``w='4.6cm'``.
881+
882+
h: float or string
883+
As above, if a float it is the hieght of the figure in inches,
884+
or a string decoded as above.
885+
886+
forward : bool, default True
887+
Forward to the window so the canvas size is updated;
888+
e.g., you can resize the figure window from the shell
889+
890+
See Also
891+
--------
892+
matplotlib.Figure.set_size_inches
893+
894+
"""
895+
if h is None:
896+
w, h = w
897+
if isinstance(w, str):
898+
w = cbook._print_unit(w)
899+
if isinstance(w, str):
900+
raise ValueError('Could not convert str {} to '
901+
'inches'.format(w))
902+
if isinstance(h, str):
903+
h = cbook._print_unit(h)
904+
if isinstance(h, str):
905+
raise ValueError('Could not convert str {} to '
906+
'inches'.format(h))
907+
self.set_size_inches(w, h, forward=forward)
908+
865909
def set_size_inches(self, w, h=None, forward=True):
866-
"""Set the figure size in inches.
910+
"""Set the figure size.
867911
868912
Call signatures::
869913
870914
fig.set_size_inches(w, h) # OR
871915
fig.set_size_inches((w, h))
872916
873-
optional kwarg *forward=True* will cause the canvas size to be
917+
Optional kwarg *forward=True* will cause the canvas size to be
874918
automatically updated; e.g., you can resize the figure window
875919
from the shell
876920
877-
ACCEPTS: a (w, h) tuple with w, h in inches
921+
ACCEPTS: a (w, h) tuple with w, h in inches.
878922
879923
See Also
880924
--------
@@ -883,8 +927,10 @@ def set_size_inches(self, w, h=None, forward=True):
883927

884928
# the width and height have been passed in as a tuple to the first
885929
# argument, so unpack them
930+
886931
if h is None:
887932
w, h = w
933+
888934
if not all(np.isfinite(_) for _ in (w, h)):
889935
raise ValueError('figure size must be finite not '
890936
'({}, {})'.format(w, h))

0 commit comments

Comments
 (0)
0