Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f7c55b

Browse files
committed
Switch AxLine.set_xy{1,2} to take a single argument.
1 parent 54d718e commit 1f7c55b

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``AxLine`` ``xy1`` and ``xy2`` setters
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
These setters now each take a single argument, ``xy1`` or ``xy2`` as a tuple.
4+
The old form, where ``x`` and ``y`` were passed as separate arguments, is
5+
deprecated.

lib/matplotlib/lines.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,18 +1553,28 @@ def get_slope(self):
15531553
"""Return the *slope* value of the line."""
15541554
return self._slope
15551555

1556-
def set_xy1(self, x, y):
1556+
def set_xy1(self, *args, **kwargs):
15571557
"""
15581558
Set the *xy1* value of the line.
15591559
15601560
Parameters
15611561
----------
1562-
x, y : float
1562+
xy1 : tuple[float, float]
15631563
Points for the line to pass through.
15641564
"""
1565-
self._xy1 = x, y
1565+
params = _api.select_matching_signature([
1566+
lambda self, x, y: locals(), lambda self, xy1: locals(),
1567+
], self, *args, **kwargs)
1568+
if "x" in params:
1569+
_api.warn_deprecated("3.10", message=(
1570+
"Passing x and y separately to AxLine.set_xy1 is deprecated since "
1571+
"%(since)s; pass them as a single tuple instead."))
1572+
xy1 = params["x"], params["y"]
1573+
else:
1574+
xy1 = params["xy1"]
1575+
self._xy1 = xy1
15661576

1567-
def set_xy2(self, x, y):
1577+
def set_xy2(self, *args, **kwargs):
15681578
"""
15691579
Set the *xy2* value of the line.
15701580
@@ -1576,11 +1586,21 @@ def set_xy2(self, x, y):
15761586
15771587
Parameters
15781588
----------
1579-
x, y : float
1589+
xy2 : tuple[float, float]
15801590
Points for the line to pass through.
15811591
"""
15821592
if self._slope is None:
1583-
self._xy2 = x, y
1593+
params = _api.select_matching_signature([
1594+
lambda self, x, y: locals(), lambda self, xy2: locals(),
1595+
], self, *args, **kwargs)
1596+
if "x" in params:
1597+
_api.warn_deprecated("3.10", message=(
1598+
"Passing x and y separately to AxLine.set_xy2 is deprecated since "
1599+
"%(since)s; pass them as a single tuple instead."))
1600+
xy2 = params["x"], params["y"]
1601+
else:
1602+
xy2 = params["xy2"]
1603+
self._xy2 = xy2
15841604
else:
15851605
raise ValueError("Cannot set an 'xy2' value while 'slope' is set;"
15861606
" they differ but their functionalities overlap")

lib/matplotlib/lines.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ class AxLine(Line2D):
130130
def get_xy1(self) -> tuple[float, float] | None: ...
131131
def get_xy2(self) -> tuple[float, float] | None: ...
132132
def get_slope(self) -> float: ...
133-
def set_xy1(self, x: float, y: float) -> None: ...
134-
def set_xy2(self, x: float, y: float) -> None: ...
133+
def set_xy1(self, xy1: tuple[float, float]) -> None: ...
134+
def set_xy2(self, xy2: tuple[float, float]) -> None: ...
135135
def set_slope(self, slope: float) -> None: ...
136136

137137
class VertexSelector:

lib/matplotlib/tests/test_lines.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,16 +417,20 @@ def test_axline_setters():
417417
line2 = ax.axline((.1, .1), (.8, .4))
418418
# Testing xy1, xy2 and slope setters.
419419
# This should not produce an error.
420-
line1.set_xy1(.2, .3)
420+
line1.set_xy1((.2, .3))
421421
line1.set_slope(2.4)
422-
line2.set_xy1(.3, .2)
423-
line2.set_xy2(.6, .8)
422+
line2.set_xy1((.3, .2))
423+
line2.set_xy2((.6, .8))
424424
# Testing xy1, xy2 and slope getters.
425425
# Should return the modified values.
426426
assert line1.get_xy1() == (.2, .3)
427427
assert line1.get_slope() == 2.4
428428
assert line2.get_xy1() == (.3, .2)
429429
assert line2.get_xy2() == (.6, .8)
430+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
431+
line1.set_xy1(.2, .3)
432+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
433+
line2.set_xy2(.6, .8)
430434
# Testing setting xy2 and slope together.
431435
# These test should raise a ValueError
432436
with pytest.raises(ValueError,

0 commit comments

Comments
 (0)