By default, image viewers cast NaNs to zero before applying a colormap. This prevents the use of the matplotlib "bad" feature. For each colormap, values above or below the colormap vmin/vmax are rendered with an optionally specified value, and "bad" values (usually nans) are rendered by a third value:
import matplotlib.pyplot as plt
plt.cm.viridis
By default, bad is color='k', alpha=0, but this can be changed with:
plt.cm.viridis.set_bad(color='r')
which allows users to clearly visualize nans efficiently and without special handling.
This PR allows the user to set a private attribute on the CompositeArray class called _set_nans_to_zero. When True, the behavior is unchanged. When set to False by a user, nans are not forced to zero, and the bad values are rendered as described by the mpl colormap. An example is shown below using Imviz from jdaviz:
Description
By default, image viewers cast NaNs to zero before applying a colormap. This prevents the use of the matplotlib "bad" feature. For each colormap, values above or below the colormap vmin/vmax are rendered with an optionally specified value, and "bad" values (usually nans) are rendered by a third value:
By default,
bad
iscolor='k', alpha=0
, but this can be changed with:which allows users to clearly visualize nans efficiently and without special handling.
Glue currently forces nans to zero before they are converted to colors by the colormap: https://github.com/glue-viz/glue/blob/0f83a03c997b78476043485f4145f6e69cb8a8cf/glue/viewers/image/composite_array.py#L140 which prevents the user from ever making use of the
bad
functionality.This PR allows the user to set a private attribute on the
CompositeArray
class called_set_nans_to_zero
. When True, the behavior is unchanged. When set to False by a user, nans are not forced to zero, and thebad
values are rendered as described by the mpl colormap. An example is shown below using Imviz from jdaviz: