Skip to content

Commit 9a27b74

Browse files
authored
Merge pull request #16364 from meeseeksmachine/auto-backport-of-pr-16344-on-v3.2.x
Backport PR #16344 on branch v3.2.x (Cast vmin/vmax to floats before nonsingular-expanding them.)
2 parents 89ff308 + 5d99e15 commit 9a27b74

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/matplotlib/tests/test_colorbar.py

+11
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,14 @@ def test_colorbar_label():
580580

581581
cbar3 = fig.colorbar(im, orientation='horizontal', label='horizontal cbar')
582582
assert cbar3.ax.get_xlabel() == 'horizontal cbar'
583+
584+
585+
@pytest.mark.parametrize("clim", [(-20000, 20000), (-32768, 0)])
586+
def test_colorbar_int(clim):
587+
# Check that we cast to float early enough to not
588+
# overflow ``int16(20000) - int16(-20000)`` or
589+
# run into ``abs(int16(-32768)) == -32768``.
590+
fig, ax = plt.subplots()
591+
im = ax.imshow([[*map(np.int16, clim)]])
592+
fig.colorbar(im)
593+
assert (im.norm.vmin, im.norm.vmax) == clim

lib/matplotlib/transforms.py

+4
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,10 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
28122812
vmin, vmax = vmax, vmin
28132813
swapped = True
28142814

2815+
# Expand vmin, vmax to float: if they were integer types, they can wrap
2816+
# around in abs (abs(np.int8(-128)) == -128) and vmax - vmin can overflow.
2817+
vmin, vmax = map(float, [vmin, vmax])
2818+
28152819
maxabsvalue = max(abs(vmin), abs(vmax))
28162820
if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
28172821
vmin = -expander

0 commit comments

Comments
 (0)