Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f9b45d

Browse files
committed
ENH: fig.set_size to allow non-inches units
1 parentb96fb6f commit2f9b45d

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
returnreport
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+
returnst
2090+
unit=st[-2:]
2091+
ifunitinprint_units:
2092+
returnnum/print_units[unit]
2093+
else:
2094+
# let the parent handle the errors
2095+
returnst

‎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
returnim
864864

865+
defset_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+
ifhisNone:
896+
w,h=w
897+
ifisinstance(w,str):
898+
w=cbook._print_unit(w)
899+
ifisinstance(w,str):
900+
raiseValueError('Could not convert str {} to '
901+
'inches'.format(w))
902+
ifisinstance(h,str):
903+
h=cbook._print_unit(h)
904+
ifisinstance(h,str):
905+
raiseValueError('Could not convert str {} to '
906+
'inches'.format(h))
907+
self.set_size_inches(w,h,forward=forward)
908+
865909
defset_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
ifhisNone:
887932
w,h=w
933+
888934
ifnotall(np.isfinite(_)for_in (w,h)):
889935
raiseValueError('figure size must be finite not '
890936
'({}, {})'.format(w,h))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp