Fix NoNorm cursor formatting for uint8 images by goutamadwant · Pull Request #31967 · matplotlib/matplotlib · GitHub
Skip to content

Fix NoNorm cursor formatting for uint8 images#31967

Open
goutamadwant wants to merge 1 commit into
matplotlib:mainfrom
goutamadwant:fix-nonorm-cursor-overflow
Open

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

Conversation

@goutamadwant

@goutamadwant goutamadwant commented Jun 28, 2026

Copy link
Copy Markdown

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

@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
Author

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
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

3 participants