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

Skip to content
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
View file Open in desktop
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
matplotlib.use('TkAgg')
plt.axline(xy1=(0, 0), slope=1E-8)
plt.show()

# compare

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
plt.axline(xy1=(0, 0), slope=1.1E-8)
plt.show()
0