Using imshow with Quantity, LogNorm, and colorbar crashes · Issue #11306 · astropy/astropy · GitHub
Skip to content

Using imshow with Quantity, LogNorm, and colorbar crashes #11306

Description

@dstansby

Trying to use imshow() with a Quantity and LogNorm, and then trying to draw a colorbar doesn't work. I suspect this needs to be fixed in matplotlib, but thought I would open it here too just in case.

A simple example is:

import astropy.units as u
import matplotlib.colors as mcolor
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(2, 2) * u.m

fig, ax = plt.subplots()
im = ax.imshow(data, norm=mcolor.LogNorm())
fig.colorbar(im)

plt.show()

which fails with

Traceback (most recent call last):
  File "/Users/dstansby/github/matplotlib/examples/units/units_imshow.py", line 9, in <module>
    im = ax.imshow(data, norm=mcolor.LogNorm())
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/__init__.py", line 1348, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/axes/_axes.py", line 5591, in imshow
    im._scale_norm(norm, vmin, vmax)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/cm.py", line 291, in _scale_norm
    self.autoscale_None()
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/cm.py", line 476, in autoscale_None
    self.norm.autoscale_None(self._A)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/colors.py", line 1522, in autoscale_None
    super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.9/site-packages/numpy/ma/core.py", line 2043, in masked_less_equal
    return masked_where(less_equal(x, value), x, copy=copy)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.9/site-packages/numpy/ma/core.py", line 1932, in masked_where
    cond = mask_or(cond, a._mask)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.9/site-packages/numpy/ma/core.py", line 1742, in mask_or
    return make_mask(m1, copy=copy, shrink=shrink, dtype=dtype)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.9/site-packages/numpy/ma/core.py", line 1635, in make_mask
    result = _shrink_mask(result)
  File "/Users/dstansby/miniconda3/envs/dev/lib/python3.9/site-packages/numpy/ma/core.py", line 1544, in _shrink_mask
    if m.dtype.names is None and not m.any():
  File "/Users/dstansby/github/astropy/astropy/units/quantity.py", line 1645, in any
    raise TypeError("cannot evaluate truth value of quantities. "
TypeError: cannot evaluate truth value of quantities. Evaluate array with q.value.any(...)

This is present on both (Matplotlib master, astropy master) and (Matplotlib 3.3.4, astropy 4.2) with the same error message.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions