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

Skip to content

Commit fd183f4

Browse files
authored
Merge pull request matplotlib#26160 from saranti/axlinesetter
add setters and getters for _AxLine's xy1, xy2 and slope parameters
2 parents e8bfdcb + 6a3157e commit fd183f4

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

doc/api/lines_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Classes
1717

1818
Line2D
1919
VertexSelector
20+
AxLine
2021

2122
Functions
2223
---------
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Axline setters and getters
2+
--------------------------
3+
4+
The returned object from `.axes.Axes.axline` now supports getter and setter
5+
methods for its *xy1*, *xy2* and *slope* attributes:
6+
7+
.. code-block:: python
8+
9+
line1.get_xy1()
10+
line1.get_slope()
11+
line2.get_xy2()
12+
13+
.. code-block:: python
14+
15+
line1.set_xy1(.2, .3)
16+
line1.set_slope(2.4)
17+
line2.set_xy2(.1, .6)

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def axline(self, xy1, xy2=None, *, slope=None, **kwargs):
920920
# data limits should not be adjusted.
921921
datalim = []
922922

923-
line = mlines._AxLine(xy1, xy2, slope, **kwargs)
923+
line = mlines.AxLine(xy1, xy2, slope, **kwargs)
924924
# Like add_line, but correctly handling data limits.
925925
self._set_artist_props(line)
926926
if line.get_clip_path() is None:

lib/matplotlib/lines.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,13 +1464,24 @@ def is_dashed(self):
14641464
return self._linestyle in ('--', '-.', ':')
14651465

14661466

1467-
class _AxLine(Line2D):
1467+
class AxLine(Line2D):
14681468
"""
14691469
A helper class that implements `~.Axes.axline`, by recomputing the artist
14701470
transform at draw time.
14711471
"""
14721472

14731473
def __init__(self, xy1, xy2, slope, **kwargs):
1474+
"""
1475+
Parameters
1476+
----------
1477+
xy1 : (float, float)
1478+
The first set of (x, y) coordinates for the line to pass through.
1479+
xy2 : (float, float) or None
1480+
The second set of (x, y) coordinates for the line to pass through.
1481+
Either *xy2* or *slope* has to be given.
1482+
slope : float or None
1483+
The slope of the line. Either *xy2* or *slope* has to be given.
1484+
"""
14741485
super().__init__([0, 1], [0, 1], **kwargs)
14751486

14761487
if (xy2 is None and slope is None or
@@ -1527,6 +1538,65 @@ def draw(self, renderer):
15271538
self._transformed_path = None # Force regen.
15281539
super().draw(renderer)
15291540

1541+
def get_xy1(self):
1542+
"""
1543+
Return the *xy1* value of the line.
1544+
"""
1545+
return self._xy1
1546+
1547+
def get_xy2(self):
1548+
"""
1549+
Return the *xy2* value of the line.
1550+
"""
1551+
return self._xy2
1552+
1553+
def get_slope(self):
1554+
"""
1555+
Return the *slope* value of the line.
1556+
"""
1557+
return self._slope
1558+
1559+
def set_xy1(self, x, y):
1560+
"""
1561+
Set the *xy1* value of the line.
1562+
1563+
Parameters
1564+
----------
1565+
x, y : float
1566+
Points for the line to pass through.
1567+
"""
1568+
self._xy1 = x, y
1569+
1570+
def set_xy2(self, x, y):
1571+
"""
1572+
Set the *xy2* value of the line.
1573+
1574+
Parameters
1575+
----------
1576+
x, y : float
1577+
Points for the line to pass through.
1578+
"""
1579+
if self._slope is None:
1580+
self._xy2 = x, y
1581+
else:
1582+
raise ValueError("Cannot set an 'xy2' value while 'slope' is set;"
1583+
" they differ but their functionalities overlap")
1584+
1585+
def set_slope(self, slope):
1586+
"""
1587+
Set the *slope* value of the line.
1588+
1589+
Parameters
1590+
----------
1591+
slope : float
1592+
The slope of the line.
1593+
"""
1594+
if self._xy2 is None:
1595+
self._slope = slope
1596+
else:
1597+
raise ValueError("Cannot set a 'slope' value while 'xy2' is set;"
1598+
" they differ but their functionalities overlap")
1599+
15301600

15311601
class VertexSelector:
15321602
"""

lib/matplotlib/lines.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,20 @@ class Line2D(Artist):
120120
def get_solid_capstyle(self) -> Literal["butt", "projecting", "round"]: ...
121121
def is_dashed(self) -> bool: ...
122122

123-
class _AxLine(Line2D):
123+
class AxLine(Line2D):
124124
def __init__(
125125
self,
126126
xy1: tuple[float, float],
127127
xy2: tuple[float, float] | None,
128128
slope: float | None,
129129
**kwargs
130130
) -> None: ...
131+
def get_xy1(self) -> tuple[float, float] | None: ...
132+
def get_xy2(self) -> tuple[float, float] | None: ...
133+
def get_slope(self) -> float: ...
134+
def set_xy1(self, x: float, y: float) -> None: ...
135+
def set_xy2(self, x: float, y: float) -> None: ...
136+
def set_slope(self, slope: float) -> None: ...
131137

132138
class VertexSelector:
133139
axes: Axes

lib/matplotlib/tests/test_lines.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,30 @@ def test_markevery_prop_cycle(fig_test, fig_ref):
409409
ax = fig_test.add_subplot()
410410
for i, _ in enumerate(cases):
411411
ax.plot(y - i, 'o-')
412+
413+
414+
def test_axline_setters():
415+
fig, ax = plt.subplots()
416+
line1 = ax.axline((.1, .1), slope=0.6)
417+
line2 = ax.axline((.1, .1), (.8, .4))
418+
# Testing xy1, xy2 and slope setters.
419+
# This should not produce an error.
420+
line1.set_xy1(.2, .3)
421+
line1.set_slope(2.4)
422+
line2.set_xy1(.3, .2)
423+
line2.set_xy2(.6, .8)
424+
# Testing xy1, xy2 and slope getters.
425+
# Should return the modified values.
426+
assert line1.get_xy1() == (.2, .3)
427+
assert line1.get_slope() == 2.4
428+
assert line2.get_xy1() == (.3, .2)
429+
assert line2.get_xy2() == (.6, .8)
430+
# Testing setting xy2 and slope together.
431+
# These test should raise a ValueError
432+
with pytest.raises(ValueError,
433+
match="Cannot set an 'xy2' value while 'slope' is set"):
434+
line1.set_xy2(.2, .3)
435+
436+
with pytest.raises(ValueError,
437+
match="Cannot set a 'slope' value while 'xy2' is set"):
438+
line2.set_slope(3)

0 commit comments

Comments
 (0)