Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Sep 27, 2024

Conversation

kyracho
Copy link
Contributor

@kyracho kyracho commented Sep 25, 2024

PR summary

Hello, this PR closes #28386 by replacing if np.isclose(slope, 0) with if slope == 0 in lines.py, allowing for better resolution of small slopes with axlines. Additionally, I have added the test_line_slope function in test_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

line = ax.axline(xy1=(0, 0), slope=slope)

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

@timhoffm timhoffm Sep 25, 2024

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.

Copy link
Contributor Author

@kyracho kyracho Sep 27, 2024

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!

@kyracho kyracho force-pushed the final-bugfix-issue-28386 branch from 6baf2c2 to ebcf5f2 Compare September 26, 2024 23:46
@QuLogic QuLogic merged commit e90952f into matplotlib:main Sep 27, 2024
42 of 43 checks passed
@QuLogic QuLogic added this to the v3.9.3 milestone Sep 27, 2024
@QuLogic
Copy link
Member

QuLogic commented Sep 27, 2024

@meeseeksdev backport to v3.9.x

@QuLogic
Copy link
Member

QuLogic commented Sep 27, 2024

Thanks @kyracho! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@QuLogic QuLogic mentioned this pull request Sep 28, 2024
5 tasks
@kyracho kyracho deleted the final-bugfix-issue-28386 branch September 28, 2024 01:33
QuLogic added a commit that referenced this pull request Sep 28, 2024
…881-on-v3.9.x

Backport PR #28881 on branch v3.9.x (Fix `axline` for slopes <= 1E-8. Closes #28386)
kyracho added a commit to kyracho/matplotlib that referenced this pull request Oct 10, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Minor issue - Drawing an axline sets slopes less than 1E-8 to 0
3 participants