Skip to content

Fix NoNorm cursor formatting for uint8 images - #31967

Merged
rcomer merged 1 commit into
matplotlib:mainfrom
goutamadwant:fix-nonorm-cursor-overflow
Jul 10, 2026
Merged

Fix NoNorm cursor formatting for uint8 images#31967
rcomer merged 1 commit into
matplotlib:mainfrom
goutamadwant:fix-nonorm-cursor-overflow

Conversation

@goutamadwant

@goutamadwant goutamadwant commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

PR summary

closes #31960

This fixes cursor formatting for uint8 image data displayed with colors.NoNorm().

NoNorm returns the original NumPy scalar value. With recent NumPy versions, multiplying a uint8 cursor value by the colormap size can raise OverflowError while Matplotlib computes neighboring color intervals for cursor display precision.

This change converts the normalized scalar to a Python scalar at the point where that interval arithmetic is needed, preserving the existing cursor readout behavior while avoiding the narrow-dtype overflow.

Before this change, formatting a uint8 cursor value from a NoNorm image raised OverflowError. After this change, it returns the expected cursor label.

Breaking changes: None.

Tests run:

  • python -P -m pytest lib/matplotlib/tests/test_image.py::test_format_cursor_data_uint8_no_norm lib/matplotlib/tests/test_image.py::test_format_cursor_data
  • python -P -m pytest lib/matplotlib/tests/test_image.py::test_format_cursor_data_multinorm lib/matplotlib/tests/test_artist.py::test_format_cursor_data_BoundaryNorm
  • python -P -m pytest lib/matplotlib/tests/test_image.py -k cursor_data

AI Disclosure

I used Codex to review the relevant code and understand the existing codebase.

PR checklist

@github-actions

Copy link
Copy Markdown

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.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@scottshambaugh scottshambaugh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use our PR template that auto-populates when you open a PR. Specifically, you are missing its AI Disclosure section.

Comment thread lib/matplotlib/tests/test_image.py Outdated


def test_format_cursor_data_uint8_no_norm():
from matplotlib.backend_bases import MouseEvent

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you move this import (and the others in the surrounding functions) to the top of the file instead of in-line?

@scottshambaugh scottshambaugh Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There are more of these in the file that can be cleaned up. Once that's done, this looks good to me

Comment thread lib/matplotlib/colorizer.py Outdated
@scottshambaugh scottshambaugh added this to the v3.11.1 milestone Jun 30, 2026
@goutamadwant
goutamadwant force-pushed the fix-nonorm-cursor-overflow branch from e4cb56a to 34c6aed Compare July 2, 2026 05:54
@goutamadwant

Copy link
Copy Markdown
Contributor Author

Thanks @scottshambaugh addressed the comments. I moved the MouseEvent imports to the module imports, changed the scalar conversion to the suggested int(float(normed) * n) form and also used the PR template and its AI Disclosure section. Let me know if you have any more suggestions. thanks!

Comment thread lib/matplotlib/tests/test_image.py Outdated
Convert normalized scalar values to Python scalars before computing neighboring color intervals. This avoids NumPy scalar overflow when NoNorm returns a uint8 cursor value for image data.

Add a regression test for cursor formatting on uint8 image data with NoNorm.
@goutamadwant
goutamadwant force-pushed the fix-nonorm-cursor-overflow branch from 34c6aed to 657864d Compare July 2, 2026 23:33
@rcomer
rcomer merged commit 0ced39c into matplotlib:main Jul 10, 2026
46 checks passed
rcomer added a commit that referenced this pull request Jul 10, 2026
…967-on-v3.11.x

Backport PR #31967 on branch v3.11.x (Fix NoNorm cursor formatting for uint8 images)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: imshow uint8 image with colors.NoNorm() causes OverflowError when mousing over figure

5 participants