-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix axline
for slopes <= 1E-8. Closes #28386
#28881
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
Conversation
lib/matplotlib/tests/test_lines.py
Outdated
line = ax.axline(xy1=(0, 0), slope=slope) | ||
|
||
# Extract the slope from the line's properties | ||
calculated_slope = line.get_slope() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test does not do anything. get_slope
just returns the value passed in. We cannot test the change by retrieving any AxLine properties because the change only affects the transform.
A test that would discriminate the change is
def test_axline_small_slope():
"""Test that small slopes are not coerced to zero in the transform."""
line = plt.axline((0, 0), slope=1e-15)
p1 = line.get_transform().transform_point((0, 0))
p2 = line.get_transform().transform_point((1, 1))
# y-values must be slightly different
dy = p2[1] - p1[1]
assert dy > 0
assert dy < 1e-13
It could be that you have to make the slope slightly larger 1e-15 is very close to the numerical limit and it may be that there we get to no change through the transform due to numerical precision.
Also you may need to change the limit assert dy < 1e-14
. We can choose this empirically. I've not thought the transform stack through to find out what the expected dy for the given points is.
Probably we should add this, even though I wouldn't have added such a test in the first place when writing the function because not coercing small slopes would be the canonical implementation behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Tim,
I tested with slopes of 1e-15
, 1e-14
, and 1e-13
, and I found that 1e-14
struck a good balance between being small enough to test the original issue and large enough to avoid potential floating-point precision issues.
For dy
, I set the limit to 4e-12
because the test fails around 3.84e-12
. This threshold ensures that the slope is not coerced to zero while allowing some margin for minor floating-point variations during transformation.
For example, with a slope of 1e-14, the expected dy
is about 3.84e-12
, and for a slope of 1e-13
, it increases to around 3.84e-11
. However, for very small slopes like 1e-15
, floating-point precision limitations cause a slight deviation, resulting in a slightly higher dy
(around 3.98e-13
), breaking the linear progression.
def test_axline_small_slope():
"""Test that small slopes are not coerced to zero in the transform."""
line = plt.axline((0, 0), slope=1e-14)
p1 = line.get_transform().transform_point((0, 0))
p2 = line.get_transform().transform_point((1, 1))
# y-values must be slightly different, confirming the slope is not coerced to zero
dy = p2[1] - p1[1]
assert dy > 0
assert dy < 4e-12
Does this version look good to you for final integration? Let me know if you have any suggestions.
Thanks!
6baf2c2
to
ebcf5f2
Compare
@meeseeksdev backport to v3.9.x |
Thanks @kyracho! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again. |
…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.
PR summary
Hello, this PR closes #28386 by replacing
if np.isclose(slope, 0)
withif slope == 0
inlines.py
, allowing for better resolution of small slopes withaxlines
. Additionally, I have added thetest_line_slope
function intest_lines.py
to ensure proper testing of this functionality.I apologize for any confusion caused by my multiple PRs on this issue; I'm still familiarizing myself with Git and contributing to larger codebases. I appreciate your patience. Thank you.
PR checklist