Skip to content

Commit 328f0cb

Browse files
committed
Add ability to use int like kwarg legend(loc...) for rcParams['legend.loc']
1 parent 965ad90 commit 328f0cb

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,12 @@ def validate_legend_loc(loc):
725725
726726
Parameters
727727
----------
728-
loc : str | (float, float) | str((float, float))
728+
loc : str | int | (float, float) | str((float, float))
729729
The location of the legend.
730730
731731
Returns
732732
-------
733-
loc : str | (float, float) or raise ValueError exception
733+
loc : str | int | (float, float) or raise ValueError exception
734734
The location of the legend.
735735
"""
736736
valid = [
@@ -745,6 +745,8 @@ def validate_legend_loc(loc):
745745
f"are {[*valid.values()]} | tuple(float, float) "
746746
f"| str(tuple(float, float))")
747747

748+
if isinstance(loc, int):
749+
return loc
748750
if isinstance(loc, str):
749751
loc = loc.lower()
750752
if loc in valid:
@@ -761,7 +763,9 @@ def validate_legend_loc(loc):
761763
if matches and len(tuples) == 1:
762764
loc = tuples[0]
763765
if isinstance(loc, tuple):
764-
if len(loc) == 2 and all(isinstance(e, numbers.Real) for e in loc):
766+
if len(loc) == 2 \
767+
and all(isinstance(e, numbers.Real)
768+
and float(e) >= 0.0 for e in loc):
765769
return loc
766770
raise ValueError(msg)
767771

lib/matplotlib/rcsetup.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,5 @@ def validate_hist_bins(
157157
float
158158
]: ...
159159

160-
def validate_legend_loc(loc: str | tuple[float | int]
161-
) -> str | tuple[float]: ...
160+
def validate_legend_loc(loc: str | int | tuple[float]
161+
) -> str | int | tuple[float]: ...

lib/matplotlib/tests/test_rcparams.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -598,17 +598,12 @@ def test_deprecation(monkeypatch):
598598
# mpl.RcParams({'legend.loc': value})
599599

600600
@pytest.mark.parametrize('value', [
601+
1,
601602
(0.9, .7),
602603
'(0.9, .7)'
603604
])
604605
def test_rcparams_legend_loc(value):
605606
# rcParams['legend.loc'] should allow any of the following formats.
606607
# if any of these are not allowed, an exception will be raised
607608
# test for gh issue #22338
608-
_, ax = plt.subplots(1)
609-
expected = ax.legend(value).get_bbox_to_anchor().bounds
610-
_, ax = plt.subplots(1)
611609
mpl.rcParams['legend.loc'] = value
612-
result = ax.legend().get_bbox_to_anchor().bounds
613-
614-
assert result == expected

0 commit comments

Comments
 (0)