@@ -2492,6 +2492,9 @@ def __init__(self,
24922492 - a tuple ``(width, height)``, which is interpreted in inches, i.e. as
24932493 ``(width, height, "in")``.
24942494
2495+ One of *width* or *height* may be ``None``; the respective value is
2496+ taken from :rc:`figure.figsize`.
2497+
24952498 dpi : float, default: :rc:`figure.dpi`
24962499 Dots per inch.
24972500
@@ -3155,6 +3158,10 @@ def set_size_inches(self, w, h=None, forward=True):
31553158 """
31563159 if h is None : # Got called with a single pair as argument.
31573160 w , h = w
3161+ if w is None or h is None :
3162+ raise ValueError (
3163+ "Figure.set_size_inches does not accept None; provide both "
3164+ "width and height explicitly" )
31583165 size = np .array ([w , h ])
31593166 if not np .isfinite (size ).all () or (size < 0 ).any ():
31603167 raise ValueError (f'figure size must be positive finite not { size } ' )
@@ -3763,25 +3770,40 @@ def _parse_figsize(figsize, dpi):
37633770 """
37643771 num_parts = len (figsize )
37653772 if num_parts == 2 :
3766- return figsize
3773+ x , y = figsize
37673774 elif num_parts == 3 :
37683775 x , y , unit = figsize
37693776 if unit == 'in' :
37703777 pass
37713778 elif unit == 'cm' :
3772- x /= 2.54
3773- y /= 2.54
3779+ if x is not None :
3780+ x /= 2.54
3781+ if y is not None :
3782+ y /= 2.54
37743783 elif unit == 'px' :
3775- x /= dpi
3776- y /= dpi
3784+ if x is not None :
3785+ x /= dpi
3786+ if y is not None :
3787+ y /= dpi
37773788 else :
37783789 raise ValueError (
37793790 f"Invalid unit { unit !r} in 'figsize'; "
37803791 "supported units are 'in', 'cm', 'px'"
37813792 )
3782- return x , y
37833793 else :
37843794 raise ValueError (
37853795 "Invalid figsize format, expected (x, y) or (x, y, unit) but got "
37863796 f"{ figsize !r} "
37873797 )
3798+
3799+ if x is None and y is None :
3800+ raise ValueError (
3801+ "figsize=(None, None) is invalid; at least one of width or "
3802+ "height must be provided" )
3803+
3804+ default_width , default_height = mpl .rcParams ["figure.figsize" ]
3805+ if x is None :
3806+ x = default_width
3807+ if y is None :
3808+ y = default_height
3809+ return x , y
0 commit comments