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

Skip to content

[Bug]: Imshow with all negative values leads to math domain errors. #23108

Closed
@TheSkynet1337

Description

@TheSkynet1337

Bug summary

When using imshow to display a numpy array filled with identical negative values hovering the mouse over the displayed image throws math domain errors.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

test = np.full((2, 1), -1)
plt.imshow(test)
plt.show()

Actual outcome

Hovering the mouse over the plot spews math domain errors into console and stops the value and coordinate under the mouse from being displayed in the top right.

Traceback (most recent call last):
  File "C:\Users\{User}\miniconda3\envs\cv2\lib\site-packages\matplotlib\cbook\__init__.py", line 287, in process
    func(*args, **kwargs)
  File "C:\Users\{User}\miniconda3\envs\cv2\lib\site-packages\matplotlib\backend_bases.py", line 3061, in mouse_move
    s = self._mouse_event_to_message(event)
  File "C:\Users\{User}\miniconda3\envs\cv2\lib\site-packages\matplotlib\backend_bases.py", line 3053, in _mouse_event_to_message
    data_str = a.format_cursor_data(data).rstrip()
  File "C:\Users\{User}\miniconda3\envs\cv2\lib\site-packages\matplotlib\artist.py", line 1285, in format_cursor_data
    g_sig_digits = cbook._g_sig_digits(data, delta)
  File "C:\Users\{User}\miniconda3\envs\cv2\lib\site-packages\matplotlib\cbook\__init__.py", line 2228, in _g_sig_digits
    - math.floor(math.log10(delta))) if math.isfinite(value) else 0
ValueError: math domain error

image
The coordinates and values in the top right of the window are missing.

Expected outcome

image
The value area should be displayed correctly.

Additional information

Perhaps the delta in cbook\__init__.py", line 2228, in _g_sig_digits - math.floor(math.log10(delta))) if math.isfinite(value) else 0 is negative in this case.

The errors do not occur if a single value in the array is different(negativ or positive doesn't matter).
i.e.

import matplotlib.pyplot as plt
import numpy as np

test = np.full((2, 1), -1)
test[0][0] = 0
plt.imshow(test)
plt.show()

Will not error.
The errors also do not occur if set_clim() is used.

Operating system

Windows 10 19044.1645

Matplotlib Version

3.5.1

Matplotlib Backend

QtAgg

Python version

Python 3.9.12

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions