proplot-dev / proplot

🎨 A succinct matplotlib wrapper for making beautiful, publication-quality graphics
https://proplot.readthedocs.io
MIT License
1.07k stars 96 forks source link

`imshow` fails to plot boolean arrays #409

Closed scottstanie closed 1 year ago

scottstanie commented 1 year ago

Description

imshow errors due to np.sign being applied to a boolean.

Steps to reproduce

A "Minimal, Complete and Verifiable Example" will make it much easier for maintainers to help you.

import proplot as pplt
import numpy as np
fig, ax = pplt.subplots()
ax.imshow(np.random.rand(5, 5).astype(bool))

Expected behavior: [What you expected to happen]

Actual behavior: [What actually happened]

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
Cell In[9], line 2
      1 fig, ax = pplt.subplots()
----> 2 ax.imshow(np.random.rand(5, 5).astype(bool))

File /u/aurora-r0/staniewi/repos/proplot/proplot/internals/inputs.py:292, in _preprocess_or_redirect.<locals>._decorator.<locals>._preprocess_or_redirect(self, *args, **kwargs)
    289             ureg.setup_matplotlib(True)
    291 # Call main function
--> 292 return func(self, *args, **kwargs)

File /u/aurora-r0/staniewi/repos/proplot/proplot/axes/plot.py:4150, in PlotAxes.imshow(self, z, **kwargs)
   4146 """
   4147 %(plot.imshow)s
   4148 """
   4149 kw = kwargs.copy()
-> 4150 kw = self._parse_cmap(z, default_discrete=False, **kw)
   4151 guide_kw = _pop_params(kw, self._update_guide)
   4152 m = self._call_native('imshow', z, **kw)

File /u/aurora-r0/staniewi/repos/proplot/proplot/internals/warnings.py:110, in _rename_kwargs.<locals>._decorator.<locals>._deprecate_kwargs_wrapper(*args, **kwargs)
    105         key_new = key_new.format(value)
    106     _warn_proplot(
    107         f'Keyword {key_old!r} was deprecated in version {version} and may '
    108         f'be removed in {_next_release()}. Please use {key_new!r} instead.'
    109     )
--> 110 return func_orig(*args, **kwargs)

File /u/aurora-r0/staniewi/repos/proplot/proplot/axes/plot.py:2230, in PlotAxes._parse_cmap(self, cmap, cmap_kw, c, color, colors, norm, norm_kw, extend, vmin, vmax, vcenter, discrete, default_discrete, default_cmap, skip_autolev, min_levels, plot_lines, plot_contours, *args, **kwargs)
   2226     vmin, vmax, kwargs = self._parse_level_lim(
   2227         *args, vmin=vmin, vmax=vmax, **kwargs
   2228     )
   2229     if autodiverging and vmin is not None and vmax is not None:
-> 2230         if abs(np.sign(vmax) - np.sign(vmin)) == 2:
   2231             isdiverging = True
   2232 if discrete:

UFuncTypeError: ufunc 'sign' did not contain a loop with signature matching types <class 'numpy.dtype[bool_]'> -> None

Equivalent steps in matplotlib

# your code here, if applicable
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.imshow(np.random.rand(5, 5).astype(bool))

Proplot version

Paste the results of import matplotlib; print(matplotlib.__version__); import proplot; print(proplot.version) here.

>>> import matplotlib; print(matplotlib.__version__); import proplot; print(proplot.version)
3.4.3
0.0.post4012

(I just forked the latest. Seems like the tags aren't visible, so setuptools_scm doesn't like it)

scottstanie commented 1 year ago

I'm not sure if this is the best way to fix it, but adding a simple conversion to uint8 like https://github.com/proplot-dev/proplot/pull/410 stops the error