@@ -44,6 +44,10 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):
44
44
relative height of ``height_ratios[i] / sum(height_ratios)``.
45
45
If not given, all rows will have the same height.
46
46
"""
47
+ if not isinstance (nrows , Integral ) or nrows <= 0 :
48
+ raise ValueError (f"Number of rows must be > 0, not { nrows } " )
49
+ if not isinstance (ncols , Integral ) or ncols <= 0 :
50
+ raise ValueError (f"Number of columns must be > 0, not { ncols } " )
47
51
self ._nrows , self ._ncols = nrows , ncols
48
52
self .set_height_ratios (height_ratios )
49
53
self .set_width_ratios (width_ratios )
@@ -642,36 +646,46 @@ def _from_subplot_args(figure, args):
642
646
- a `.SubplotSpec` -- returned as is;
643
647
- one or three numbers -- a MATLAB-style subplot specifier.
644
648
"""
649
+ message = ("Passing non-integers as three-element position "
650
+ "specification is deprecated since %(since)s and will be "
651
+ "removed %(removal)s." )
645
652
if len (args ) == 1 :
646
653
arg , = args
647
654
if isinstance (arg , SubplotSpec ):
648
655
return arg
649
656
else :
657
+ if not isinstance (arg , Integral ):
658
+ cbook .warn_deprecated ("3.3" , message = message )
659
+ arg = str (arg )
650
660
try :
651
- s = str (int (arg ))
652
- rows , cols , num = map ( int , s )
653
- except ValueError as err :
654
- raise ValueError ( "Single argument to subplot must be a "
655
- "3-digit integer" ) from err
661
+ rows , cols , num = map (int , str (arg ))
662
+ except ValueError :
663
+ raise ValueError (
664
+ f "Single argument to subplot must be a three-digit "
665
+ f"integer, not { arg } " ) from None
656
666
# num - 1 for converting from MATLAB to python indexing
657
667
return GridSpec (rows , cols , figure = figure )[num - 1 ]
658
668
elif len (args ) == 3 :
659
669
rows , cols , num = args
660
- rows = int (rows )
661
- cols = int (cols )
662
- if rows <= 0 :
663
- raise ValueError (f"Number of rows must be > 0, not { rows } " )
664
- if cols <= 0 :
665
- raise ValueError (f"Number of columns must be > 0, not { cols } " )
670
+ if not (isinstance (rows , Integral ) and isinstance (cols , Integral )):
671
+ cbook .warn_deprecated ("3.3" , message = message )
672
+ rows , cols = map (int , [rows , cols ])
673
+ gs = GridSpec (rows , cols , figure = figure )
666
674
if isinstance (num , tuple ) and len (num ) == 2 :
667
- i , j = map (int , num )
668
- return GridSpec (rows , cols , figure = figure )[i - 1 :j ]
675
+ if not all (isinstance (n , Integral ) for n in num ):
676
+ cbook .warn_deprecated ("3.3" , message = message )
677
+ i , j = map (int , num )
678
+ else :
679
+ i , j = num
680
+ return gs [i - 1 :j ]
669
681
else :
682
+ if not isinstance (num , Integral ):
683
+ cbook .warn_deprecated ("3.3" , message = message )
684
+ num = int (num )
670
685
if num < 1 or num > rows * cols :
671
686
raise ValueError (
672
687
f"num must be 1 <= num <= { rows * cols } , not { num } " )
673
- # num - 1 for converting from MATLAB to python indexing
674
- return GridSpec (rows , cols , figure = figure )[int (num ) - 1 ]
688
+ return gs [num - 1 ] # -1 due to MATLAB indexing.
675
689
else :
676
690
raise TypeError (f"subplot() takes 1 or 3 positional arguments but "
677
691
f"{ len (args )} were given" )
0 commit comments