File tree 2 files changed +29
-0
lines changed 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -472,6 +472,14 @@ def val_or_rc(val, rc_name):
472
472
loc = 'upper right'
473
473
if isinstance (loc , str ):
474
474
loc = _api .check_getitem (self .codes , loc = loc )
475
+ elif isinstance (loc , tuple ):
476
+ if len (loc ) != 2 :
477
+ raise ValueError (
478
+ "loc must be string or pair of floats" )
479
+ for loc_elt in loc :
480
+ # int can be converted to float, so ints are fine
481
+ if not isinstance (loc_elt , (float , int )):
482
+ raise ValueError (f"{ loc_elt } is not a valid value for loc" )
475
483
if not self .isaxes and loc == 0 :
476
484
raise ValueError (
477
485
"Automatic legend placement (loc='best') not implemented for "
Original file line number Diff line number Diff line change @@ -1090,3 +1090,24 @@ def test_ncol_ncols(fig_test, fig_ref):
1090
1090
ncols = 3
1091
1091
fig_test .legend (strings , ncol = ncols )
1092
1092
fig_ref .legend (strings , ncols = ncols )
1093
+
1094
+
1095
+ def test_loc_invalid_tuple_exception ():
1096
+ # check that exception is raised if the loc arg
1097
+ # of legend is not a 2-tuple of numbers
1098
+ fig , ax = plt .subplots ()
1099
+ with pytest .raises (ValueError ,
1100
+ match = "loc must be string or pair of floats" ):
1101
+ ax .legend (loc = (1.1 , ))
1102
+ with pytest .raises (ValueError ,
1103
+ match = "loc must be string or pair of floats" ):
1104
+ ax .legend (loc = (0.481 , 0.4227 , 0.4523 ))
1105
+ with pytest .raises (ValueError ,
1106
+ match = "go blue is not a valid value for loc" ):
1107
+ ax .legend (loc = (0.481 , "go blue" ))
1108
+
1109
+
1110
+ def test_loc_valid_tuple ():
1111
+ fig , ax = plt .subplots ()
1112
+ ax .legend (loc = (0.481 , 0.442 ))
1113
+ ax .legend (loc = (1 , 2 ))
You can’t perform that action at this time.
0 commit comments