Skip to content

Commit 9131bba

Browse files
rcomermeeseeksmachine
authored andcommitted
Backport PR matplotlib#31967: Fix NoNorm cursor formatting for uint8 images
1 parent 75c03b3 commit 9131bba

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

lib/matplotlib/colorizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ def _sig_digits_from_norm(norm, data, n):
505505
delta = np.abs(norm.vmin * .1)
506506
else:
507507
# Midpoints of neighboring color intervals.
508-
neighbors = norm.inverse((int(normed * n) + np.array([0, 1])) / n)
508+
neighbors = norm.inverse(
509+
(int(float(normed) * n) + np.array([0, 1])) / n)
509510
delta = abs(neighbors - data).max()
510511

511512
g_sig_digits = cbook._g_sig_digits(data, delta)

lib/matplotlib/tests/test_image.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import matplotlib as mpl
1515
from matplotlib import (
1616
colors, image as mimage, patches, pyplot as plt, style, rcParams)
17+
from matplotlib.backend_bases import MouseEvent
1718
from matplotlib.image import (AxesImage, BboxImage, FigureImage,
1819
NonUniformImage, PcolorImage)
1920
from matplotlib.patches import Rectangle
@@ -455,8 +456,6 @@ def test_cursor_data_nonuniform(xy, data):
455456
([[0, 0]], "[0.00]"),
456457
])
457458
def test_format_cursor_data(data, text):
458-
from matplotlib.backend_bases import MouseEvent
459-
460459
fig, ax = plt.subplots()
461460
im = ax.imshow(data)
462461

@@ -465,14 +464,23 @@ def test_format_cursor_data(data, text):
465464
assert im.format_cursor_data(im.get_cursor_data(event)) == text
466465

467466

467+
def test_format_cursor_data_uint8_no_norm():
468+
data = np.array([[0, 44]], dtype=np.uint8)
469+
fig, ax = plt.subplots()
470+
im = ax.imshow(data, norm=colors.NoNorm())
471+
472+
xdisp, ydisp = ax.transData.transform([1, 0])
473+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
474+
assert im.format_cursor_data(im.get_cursor_data(event)) == "[44.000]"
475+
476+
468477
@pytest.mark.parametrize(
469478
"data, text", [
470479
([[[10001, 10000]], [[0, 0]]], "[10001.000, 0.000]"),
471480
([[[.123, .987]], [[0.1, 0]]], "[0.123, 0.100]"),
472481
([[[np.nan, 1, 2]], [[0, 0, 0]]], "[]"),
473482
])
474483
def test_format_cursor_data_multinorm(data, text):
475-
from matplotlib.backend_bases import MouseEvent
476484
fig, ax = plt.subplots()
477485
cmap_bivar = mpl.bivar_colormaps['BiOrangeBlue']
478486
cmap_multivar = mpl.multivar_colormaps['2VarAddA']

0 commit comments

Comments
 (0)