8000 Fix `axline` for slopes < 1E-8 by Andresporcruz · Pull Request #28431 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix axline for slopes < 1E-8 #28431

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 3 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
17 changes: 14 additions & 3 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,13 +1496,13 @@ def __init__(self, xy1, xy2, slope, **kwargs):
self._xy2 = xy2

def get_transform(self):
print("Executing get_transform")
ax = self.axes
points_transform = self._transform - ax.transData + ax.transScale

if self._xy2 is not None:
# two points were given
(x1, y1), (x2, y2) = \
points_transform.transform([self._xy1, self._xy2])
(x1, y1), (x2, y2) = points_transform.transform([self._xy1, self._xy2])
dx = x2 - x1
dy = y2 - y1
if np.allclose(x1, x2):
Expand All @@ -1518,9 +1518,20 @@ def get_transform(self):
x1, y1 = points_transform.transform(self._xy1)
slope = self._slope
(vxlo, vylo), (vxhi, vyhi) = ax.transScale.transform(ax.viewLim)

# Adjust the tolerance for np.isclose based on the view limits
y_limits = ax.get_ylim()
tolerance = abs((y_limits[1] - y_limits[0]) / (vxhi - vxlo)) * 1e-8

print(f"Calculated tolerance: {tolerance}")
print(f"Calculated slope: {slope}")
print(f"Y limits: {y_limits}")
print(f"View limits: {vxlo}, {vylo}, {vxhi}, {vyhi}")
Comment on lines +1526 to +1529
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel that this information is critical, please instead make it a logging message https://matplotlib.org/devdocs/devel/coding_guide.html#using-logging-for-debug-messages

But is this information useful now that you've fixed this bug?


# General case: find intersections with view limits in either
# direction, and draw between the middle two points.
if np.isclose(slope, 0):
if np.isclose(slope, 0, atol=tolerance):
print("Slope considered close to zero")
start = vxlo, y1
stop = vxhi, y1
elif np.isinf(slope):
Expand Down
13 changes: 13 additions & 0 deletions test_axline_modified.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import matplotlib
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new test file, please add another test to the axline tests

@check_figures_equal()

import matplotlib.pyplot as plt

Check failure on line 2 in test_axline_modified.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 W291 trailing whitespace Raw Output: ./test_axline_modified.py:2:32: W291 trailing whitespace
matplotlib.use('TkAgg')
plt.axline(xy1=(0, 0), slope=1E-8)
plt.show()

# compare

import matplotlib

Check failure on line 9 in test_axline_modified.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E402 module level import not at top of file Raw Output: ./test_axline_modified.py:9:1: E402 module level import not at top of file
import matplotlib.pyplot as plt

Check failure on line 10 in test_axline_modified.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E402 module level import not at top of file Raw Output: ./test_axline_modified.py:10:1: E402 module level import not at top of file

Check failure on line 10 in test_axline_modified.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 W291 trailing whitespace Raw Output: ./test_axline_modified.py:10:32: W291 trailing whitespace
matplotlib.use('TkAgg')
plt.axline(xy1=(0, 0), slope=1.1E-8)
plt.show()

Check failure on line 13 in test_axline_modified.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 W292 no newline at end of file Raw Output: ./test_axline_modified.py:13:11: W292 no newline at end of file
Loading
0