Skip to content

Commit e90952f

Browse files
authored
Fix axline for slopes <= 1E-8. Closes matplotlib#28386 (matplotlib#28881)
Hello, this PR closes matplotlib#28386 by replacing `if np.isclose(slope, 0)` with `if slope == 0` in `lines.py`, allowing for better resolution of small slopes with `axline`s. Additionally, I have added the `test_line_slope` function in `test_lines.py` to ensure proper testing of this functionality.
1 parent 7722dea commit e90952f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/matplotlib/lines.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ def get_transform(self):
15211521
(vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim)
15221522
# General case: find intersections with view limits in either
15231523
# direction, and draw between the middle two points.
1524-
if np.isclose(slope, 0):
1524+
if slope == 0:
15251525
start = vxlo, y1
15261526
stop = vxhi, y1
15271527
elif np.isinf(slope):

lib/matplotlib/tests/test_lines.py

+11
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,14 @@ def test_axline_setters():
436436
with pytest.raises(ValueError,
437437
match="Cannot set a 'slope' value while 'xy2' is set"):
438438
line2.set_slope(3)
439+
440+
441+
def test_axline_small_slope():
442+
"""Test that small slopes are not coerced to zero in the transform."""
443+
line = plt.axline((0, 0), slope=1e-14)
444+
p1 = line.get_transform().transform_point((0, 0))
445+
p2 = line.get_transform().transform_point((1, 1))
446+
# y-values must be slightly different
447+
dy = p2[1] - p1[1]
448+
assert dy > 0
449+
assert dy < 4e-12

0 commit comments

Comments
 (0)