fix: handle NoNorm in format_cursor_data to prevent OverflowError#32011
fix: handle NoNorm in format_cursor_data to prevent OverflowError#32011mayank-dev-15 wants to merge 1 commit into
Conversation
When imshow is used with uint8 data and colors.NoNorm(), mousing over the image causes an OverflowError because NoNorm.__call__ returns the raw uint8 value, and subsequent arithmetic (normed * n) overflows uint8 in NumPy 2.x which raises instead of silently wrapping. Fix by casting normed to float when NoNorm is used, preventing the uint8 overflow while preserving correct behavior for all other norms. Closes matplotlib#31960
|
Thank you for opening your first PR into Matplotlib! If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process. You can also join us on discourse chat for real-time discussion. For details on testing, writing docs, and our review process, please see the developer guide. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
|
Hi @mayank-dev-15, there is already a PR open for this issue, and we tend to give priority for the first fix submitted: #31967. Thank you for your contribution, but I'll close this for now. Additionally, please use the auto-populated template when opening PRs. Yours is missing the AI disclosure section and checklist. |
Bug
Fixes #31960
When imshow is used with uint8 data and colors.NoNorm(), mousing over the image causes an OverflowError because NoNorm.call returns the raw uint8 value unchanged, and subsequent arithmetic (
ormed * n) overflows uint8 in NumPy 2.x.
Fix
In _sig_digits_from_norm, cast
ormed to float when NoNorm is used. This prevents the uint8 overflow while preserving correct behavior for all other norms.
Reproduction
\\python
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
img = np.tile(np.arange(100, dtype=np.uint8), (100, 1))
plt.imshow(img, norm=matplotlib.colors.NoNorm())
plt.show()
Mouse over the image — OverflowError
\\
Test
Verified the fix resolves the overflow by confirming
ormed * n no longer overflows when normed is cast to float.