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

Skip to content

[Bug]: format_image_data on an image of only zeros produces a large number of zeros #28648

Closed
@trygvrad

Description

@trygvrad

Bug summary

If you use imshow, and then mouseover the image, the value is listed at the bottom of the plot.
However, if the image contains only zeros, the result is a comical number of zeros.

Code for reproduction

import matplotlib
import numpy as np
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
im = ax.imshow(np.zeros((4, 4)))
fig.show()

Actual outcome

When you mouse-over the image it shows a large number of zeros (see bottom of image).
image

Expected outcome

0.0 should be at the bottom of the page and the window should retain its original size:
image

Additional information

test_image.py→test_image_cursor_formatting() should test this perscise functionality, but there is a bug in this test where

    data = np.ma.masked_array([0], mask=[False])
    assert im.format_cursor_data(data) == '[0]'

should be

    data = np.ma.masked_array(0, mask=[False])
    assert im.format_cursor_data(data) == '[0]'

And this causes im.format_cursor_data to fallback and '0' is returned.
This test should be updated as part of a PR to fix this bug.

Operating system

No response

Matplotlib Version

matplotlib_dev, 3.8.2

Matplotlib Backend

TkAgg

Python version

No response

Jupyter version

No response

Installation

git checkout

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions