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

Commitb01a894

Browse files
committed
ENH: Support units when specifying the figsize
Reviving the spirit of#12402 and#12415, because both had significant user votes on GitHub.This PR is intentionally minimal to only expand the `figsize` parameter when creating a figure. This should be the most relevant use case. Later changing the figure size or reading it is probably less necessary. The minimal approach removes the need to track and return sizes. It is just an enhanced specification capability which directly parses to the internally used inch unit.
1 parent5defe48 commitb01a894

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

‎lib/matplotlib/figure.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,8 +2476,12 @@ def __init__(self,
24762476
"""
24772477
Parameters
24782478
----------
2479-
figsize : 2-tuple of floats, default: :rc:`figure.figsize`
2480-
Figure dimension ``(width, height)`` in inches.
2479+
figsize : (float, float) or (float, float, str), default: :rc:`figure.figsize`
2480+
The figure dimensions. This can be
2481+
2482+
- a tuple ``(width, height, unit)``, where *unit* is one of "inch", "cm",
2483+
"px".
2484+
- a tuple ``(x, y)``, which is interpreted as ``(x, y, "inch")``.
24812485
24822486
dpi : float, default: :rc:`figure.dpi`
24832487
Dots per inch.
@@ -2613,6 +2617,8 @@ def __init__(self,
26132617
edgecolor=mpl._val_or_rc(edgecolor,'figure.edgecolor')
26142618
frameon=mpl._val_or_rc(frameon,'figure.frameon')
26152619

2620+
figsize=_parse_figsize(figsize,dpi)
2621+
26162622
ifnotnp.isfinite(figsize).all()or (np.array(figsize)<0).any():
26172623
raiseValueError('figure size must be positive finite not '
26182624
f'{figsize}')
@@ -3714,3 +3720,45 @@ def figaspect(arg):
37143720
# the min/max dimensions (we don't want figures 10 feet tall!)
37153721
newsize=np.clip(newsize,figsize_min,figsize_max)
37163722
returnnewsize
3723+
3724+
3725+
def_parse_figsize(figsize,dpi):
3726+
"""
3727+
Convert a figsize expression to (width, height) in inches.
3728+
3729+
Parameters
3730+
----------
3731+
figsize : (float, float) or (float, float, str)
3732+
This can be
3733+
3734+
- a tuple ``(width, height, unit)``, where *unit* is one of "inch", "cm",
3735+
"px".
3736+
- a tuple ``(x, y)``, which is interpreted as ``(x, y, "inch")``.
3737+
3738+
dpi : float
3739+
The dots-per-inch; used for converting 'px' to 'inch'.
3740+
"""
3741+
num_parts=len(figsize)
3742+
ifnum_parts==2:
3743+
returnfigsize
3744+
elifnum_parts==3:
3745+
x,y,unit=figsize
3746+
ifunit=='inch':
3747+
pass
3748+
elifunit=='cm':
3749+
x/=2.54
3750+
y/=2.54
3751+
elifunit=='px':
3752+
x/=dpi
3753+
y/=dpi
3754+
else:
3755+
raiseValueError(
3756+
"The unit part of 'figsize' must be one of 'inch', 'cm', 'px', but "
3757+
f"found{unit!r}"
3758+
)
3759+
returnx,y
3760+
else:
3761+
raiseValueError(
3762+
"Invalid figsize format, expected (x, y) or (x, y, unit) but got "
3763+
f"{figsize!r}"
3764+
)

‎lib/matplotlib/figure.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class Figure(FigureBase):
318318
subplotpars:SubplotParams
319319
def__init__(
320320
self,
321-
figsize:tuple[float,float]|None= ...,
321+
figsize:tuple[float,float]|tuple[float,float,str]|None= ...,
322322
dpi:float|None= ...,
323323
*,
324324
facecolor:ColorType|None= ...,
@@ -419,3 +419,8 @@ class Figure(FigureBase):
419419
)->None: ...
420420

421421
deffigaspect(arg:float|ArrayLike)->tuple[float,float]: ...
422+
423+
def_parse_figsize(
424+
figsize:tuple[float,float]|tuple[float,float,Literal["inch","cm","px"]],
425+
dpi:float
426+
)->tuple[float,float]: ...

‎lib/matplotlib/pyplot.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def figure(
876876
# autoincrement if None, else integer from 1-N
877877
num:int|str|Figure|SubFigure|None=None,
878878
# defaults to rc figure.figsize
879-
figsize:tuple[float,float]|None=None,
879+
figsize:tuple[float,float]|tuple[float,float,Literal["inch","cm","px"]]|None=None,
880880
# defaults to rc figure.dpi
881881
dpi:float|None=None,
882882
*,
@@ -909,8 +909,12 @@ def figure(
909909
window title is set to this value. If num is a ``SubFigure``, its
910910
parent ``Figure`` is activated.
911911
912-
figsize : (float, float), default: :rc:`figure.figsize`
913-
Width, height in inches.
912+
figsize : (float, float) or (float, float, str), default: :rc:`figure.figsize`
913+
The figure dimensions. This can be
914+
915+
- a tuple ``(width, height, unit)``, where *unit* is one of "inch", "cm",
916+
"px".
917+
- a tuple ``(x, y)``, which is interpreted as ``(x, y, "inch")``.
914918
915919
dpi : float, default: :rc:`figure.dpi`
916920
The resolution of the figure in dots-per-inch.

‎lib/matplotlib/tests/test_figure.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
frommatplotlib.testing.decoratorsimportimage_comparison,check_figures_equal
1717
frommatplotlib.axesimportAxes
1818
frommatplotlib.backend_basesimportKeyEvent,MouseEvent
19-
frommatplotlib.figureimportFigure,FigureBase
19+
frommatplotlib.figureimport_parse_figsize,Figure,FigureBase
2020
frommatplotlib.layout_engineimport (ConstrainedLayoutEngine,
2121
TightLayoutEngine,
2222
PlaceHolderLayoutEngine)
@@ -1819,3 +1819,13 @@ def test_subfigure_stale_propagation():
18191819
sfig2.stale=True
18201820
assertsfig1.stale
18211821
assertfig.stale
1822+
1823+
1824+
@pytest.mark.parametrize("input, output", [
1825+
((6,4), (6,4)),
1826+
((6,4,"inch"), (6,4)),
1827+
((5.08,2.54,"cm"), (2,1)),
1828+
((600,400,"px"), (6,4)),
1829+
])
1830+
deftest__parse_figsize(input,output):
1831+
assert_parse_figsize(input,dpi=100)==output

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp