Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dstansby opened this issue Feb 8, 2021 · 4 comments · May be fixed by matplotlib/matplotlib#27721
Open

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

dstansby opened this issue Feb 8, 2021 · 4 comments · May be fixed by matplotlib/matplotlib#27721

Comments

@dstansby
Copy link
Contributor

dstansby commented Feb 8, 2021

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.

@pllim

This comment has been minimized.

@pllim
Copy link
Member

pllim commented Feb 8, 2021

From matplotlib/matplotlib#19476 , seems like it doesn't work without using Quantity either. Hmm.

@dstansby
Copy link
Contributor Author

dstansby commented Feb 8, 2021

Can you please provide version info? Thanks.

I've updated the original post. Yes, I don't think this is supported in Matplotlib at the moment, but I'm taking a look at the upstream issue.

@neutrinoceros
Copy link
Contributor

neutrinoceros commented Jan 22, 2024

I've opened matplotlib/matplotlib#27682 to try and fix this issue.
I expect I'll need to open a PR here too to add a test. To be discussed upstream.

@dstansby dstansby linked a pull request Jan 30, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants