Skip to content

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

Description

@crisluengo

Bug summary

I'm displaying a uint8 image, intending to show the actual values in the image (i.e. no min-max scaling as is the default). Specifying colors.NoNorm() as the norm argument is I think the recommended way to do this. The image is shown correctly. But when mousing over the image the terminal window is filled with:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.14/site-packages/matplotlib/cbook.py", line 361, in process
    func(*args, **kwargs)
    ~~~~^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 2956, in mouse_move
    self.set_message(self._mouse_event_to_message(event))
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/opt/homebrew/lib/python3.14/site-packages/matplotlib/backend_bases.py", line 2948, in _mouse_event_to_message
    data_str = a.format_cursor_data(data).rstrip()
               ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/lib/python3.14/site-packages/matplotlib/artist.py", line 1354, in format_cursor_data
    return self._format_cursor_data_override(data)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/opt/homebrew/lib/python3.14/site-packages/matplotlib/colorizer.py", line 498, in _format_cursor_data_override
    (int(normed * n) + np.array([0, 1])) / n)
         ~~~~~~~^~~
OverflowError: Python integer 256 out of bounds for uint8

I am assuming this is caused by the code that writes pixel values in the bottom-right corner of the figure window.

Code for reproduction

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

Actual outcome

The terminal is filled with repeated OverflowError messages as shown above.

Expected outcome

No error messages.

Additional information

This did not happen when I originally wrote my code 9 years ago. I can't be sure, but I think this is a fairly recent regression.

Operating system

macOS

Matplotlib Version

3.10.9 and 3.11.0

Matplotlib Backend

macosx

Python version

3.14.5

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions