BUG: fix normalizing image data contained in np.ndarray subclass by neutrinoceros · Pull Request #27682 · matplotlib/matplotlib · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/image.py
45 changes: 45 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,48 @@ def test_colorbar_format_string_and_old():
plt.imshow([[0, 1]])
cb = plt.colorbar(format="{x}%")
assert isinstance(cb._formatter, StrMethodFormatter)


def test_colorbar_log_units():
class FakeQuantity(np.ndarray):
# this is a self-contained version of astropy.units.Quantity
# reduced to a bare minimum to reproduce
# https://github.com/astropy/astropy/issues/11306

def __new__(cls, value):
return np.array(value).view(cls)

def __array_ufunc__(self, function, method, *inputs, **kwargs):
def to_value(q):
value = q.view(np.ndarray)
if value.shape:
return value
else:
return value[()]

arrays = [to_value(q) for q in inputs]
result = super().__array_ufunc__(function, method, *arrays, **kwargs)
if function not in (np.minimum, np.maximum):
return result
else:
return self._new_view(result)

def _new_view(self, obj):
obj = np.array(obj, copy=False, subok=True)
view = obj.view(FakeQuantity)
return view

def __ne__(self, other):
return NotImplemented

def __float__(self):
raise RuntimeError("boom")

def item(self, *args):
return self._new_view(super().item(*args))

data = FakeQuantity([[1, 2], [3, 4]])
fig, ax = plt.subplots()
im = ax.imshow(data, norm=LogNorm())
fig.colorbar(im)
fig.canvas.draw()
8 changes: 4 additions & 4 deletions lib/matplotlib/transforms.py