8000 Fix `axline` for slopes <= 1E-8. Closes #28386 by kyracho · Pull Request #28754 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix axline for slopes <= 1E-8. Closes #28386 #28754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,9 @@ def get_transform(self):
ax = self.axes
points_transform = self._transform - ax.transData + ax.transScale

# Initialize the manual_slope variable to False
manual_slope = False

if self._xy2 is not None:
# two points were given
(x1, y1), (x2, y2) = \
Expand All @@ -1517,10 +1520,13 @@ def get_transform(self):
# one point and a slope were given
x1, y1 = points_transform.transform(self._xy1)
slope = self._slope
# Since the slope is provided manually, set manual_slope to True
manual_slope = True
(vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim)

# General case: find intersections with view limits in either
# direction, and draw between the middle two points.
if np.isclose(slope, 0):
if not manual_slope and (np.isclose(slope, 0, atol=1E-15) or slope == 0):
start = vxlo, y1
stop = vxhi, y1
elif np.isinf(slope):
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,22 @@
with pytest.raises(ValueError,
match="Cannot set a 'slope' value while 'xy2' is set"):
line2.set_slope(3)


def test_line_slope():
slopes_to_test = [1E-8, 1E-9, 1E-10, 1E-11, 1E-12, 1E-13]

for slope in slopes_to_test:
fig, ax = plt.subplots()
line = ax.axline(xy1=(0, 0), slope=slope)

# Extract the slope from the line's properties
calculated_slope = line.get_slope()

if calculated_slope == 0:
with pytest.raises(ValueError, match="line should not be horizontal"):
raise ValueError("line should not be horizontal")

Check warning on line 453 in lib/matplotlib/tests/test_lines.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_lines.py#L452-L453

Added lines #L452 - L453 were not covered by tests
else:
print(f"Slope {slope} correctly processed as {calculated_slope}")

plt.close(fig) # Close the figure after each test to free up resources
Loading
0