Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d337f69

Browse filesBrowse files
committed
BUG: fix normalizing image data contained in np.ndarray subclass
1 parent 09eab5b commit d337f69
Copy full SHA for d337f69

File tree

3 files changed

+51
-6
lines changed
Filter options

3 files changed

+51
-6
lines changed

‎lib/matplotlib/image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
461461
vmid = np.float64(self.norm.vmin) + dv / 2
462462
fact = 1e7 if scaled_dtype == np.float64 else 1e4
463463
newmin = vmid - dv * fact
464-
if newmin < a_min:
464+
if newmin < np.float64(a_min):
465465
newmin = None
466466
else:
467467
a_min = np.float64(newmin)
468468
newmax = vmid + dv * fact
469-
if newmax > a_max:
469+
if newmax > np.float64(a_max):
470470
newmax = None
471471
else:
472472
a_max = np.float64(newmax)

‎lib/matplotlib/tests/test_colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colorbar.py
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,3 +1238,48 @@ def test_colorbar_format_string_and_old():
12381238
plt.imshow([[0, 1]])
12391239
cb = plt.colorbar(format="{x}%")
12401240
assert isinstance(cb._formatter, StrMethodFormatter)
1241+
1242+
1243+
def test_colorbar_log_units():
1244+
class FakeQuantity(np.ndarray):
1245+
# this is a self-contained version of astropy.units.Quantity
1246+
# reduced to a bare minimum to reproduce
1247+
# https://github.com/astropy/astropy/issues/11306
1248+
1249+
def __new__(cls, value):
1250+
return np.array(value).view(cls)
1251+
1252+
def __array_ufunc__(self, function, method, *inputs, **kwargs):
1253+
def to_value(q):
1254+
value = q.view(np.ndarray)
1255+
if value.shape:
1256+
return value
1257+
else:
1258+
return value[()]
1259+
1260+
arrays = [to_value(q) for q in inputs]
1261+
result = super().__array_ufunc__(function, method, *arrays, **kwargs)
1262+
if function not in (np.minimum, np.maximum):
1263+
return result
1264+
else:
1265+
return self._new_view(result)
1266+
1267+
def _new_view(self, obj):
1268+
obj = np.array(obj, copy=False, subok=True)
1269+
view = obj.view(FakeQuantity)
1270+
return view
1271+
1272+
def __ne__(self, other):
1273+
return NotImplemented
1274+
1275+
def __float__(self):
1276+
raise RuntimeError("boom")
1277+
1278+
def item(self, *args):
1279+
return self._new_view(super().item(*args))
1280+
1281+
data = FakeQuantity([[1, 2], [3, 4]])
1282+
fig, ax = plt.subplots()
1283+
im = ax.imshow(data, norm=LogNorm())
1284+
fig.colorbar(im)
1285+
fig.canvas.draw()

‎lib/matplotlib/transforms.py

Copy file name to clipboardExpand all lines: lib/matplotlib/transforms.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,15 +2853,15 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
28532853
if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
28542854
return -expander, expander
28552855

2856+
# Expand vmin, vmax to float: if they were integer types, they can wrap
2857+
# around in abs (abs(np.int8(-128)) == -128) and vmax - vmin can overflow.
2858+
vmin, vmax = map(np.float64, (vmin, vmax))
2859+
28562860
swapped = False
28572861
if vmax < vmin:
28582862
vmin, vmax = vmax, vmin
28592863
swapped = True
28602864

2861-
# Expand vmin, vmax to float: if they were integer types, they can wrap
2862-
# around in abs (abs(np.int8(-128)) == -128) and vmax - vmin can overflow.
2863-
vmin, vmax = map(float, [vmin, vmax])
2864-
28652865
maxabsvalue = max(abs(vmin), abs(vmax))
28662866
if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny:
28672867
vmin = -expander

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.