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

Error on colorbar #12445

Closed
bidhya opened this issue Oct 8, 2018 · 9 comments
Closed

Error on colorbar #12445

bidhya opened this issue Oct 8, 2018 · 9 comments
Milestone

Comments

@bidhya
Copy link

bidhya commented Oct 8, 2018

Bug report

Bug summary
I get an error on calling the colorbar() function that was working perfectly with older version of matplotlib, numpy

I suspect, this TypeError is rasied when calling the plt.colorbar() method on an image of dtype boolean.

Code for reproduction

plt.imshow(bg_img) #data type of bg_img is bool
plt.colorbar();

Actual outcome

Plots the image, but gives following error with plt.colorbar()

# If applicable, paste the console output here
#
#
TypeError                                 Traceback (most recent call last)
<ipython-input-47-384f1bb7cd5d> in <module>
      1 print(bg_img.dtype)
      2 plt.imshow(bg_img)
----> 3 plt.colorbar();

~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in colorbar(mappable, cax, ax, **kw)
   2095         ax = gca()
   2096 
-> 2097     ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
   2098     return ret
   2099 colorbar.__doc__ = matplotlib.colorbar.colorbar_doc

~\Anaconda3\lib\site-packages\matplotlib\figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   2130                              'panchor']
   2131         cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 2132         cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
   2133 
   2134         self.sca(current_ax)

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in colorbar_factory(cax, mappable, **kwargs)
   1549         cb = ColorbarPatch(cax, mappable, **kwargs)
   1550     else:
-> 1551         cb = Colorbar(cax, mappable, **kwargs)
   1552 
   1553     cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in __init__(self, ax, mappable, **kw)
   1080                 kw['alpha'] = mappable.get_alpha()
   1081 
-> 1082             ColorbarBase.__init__(self, ax, **kw)
   1083 
   1084     def on_mappable_changed(self, mappable):

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in __init__(self, ax, cmap, norm, alpha, values, boundaries, orientation, ticklocation, extend, spacing, ticks, format, drawedges, filled, extendfrac, extendrect, label)
    408         # The rest is in a method so we can recalculate when clim changes.
    409         self.config_axis()
--> 410         self.draw_all()
    411 
    412     def _extend_lower(self):

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in draw_all(self)
    432         # sets self._boundaries and self._values in real data units.
    433         # takes into account extend values:
--> 434         self._process_values()
    435         # sets self.vmin and vmax in data units, but just for
    436         # the part of the colorbar that is not part of the extend

~\Anaconda3\lib\site-packages\matplotlib\colorbar.py in _process_values(self, b)
    826                 self.norm.vmin,
    827                 self.norm.vmax,
--> 828                 expander=0.1)
    829 
    830             b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))

~\Anaconda3\lib\site-packages\matplotlib\transforms.py in nonsingular(vmin, vmax, expander, tiny, increasing)
   2900         vmax = expander
   2901 
-> 2902     elif vmax - vmin <= maxabsvalue * tiny:
   2903         if vmax == 0 and vmin == 0:
   2904             vmin = -expander

TypeError: numpy boolean subtract, the `-` operator, is deprecated, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

Expected outcome

Matplotlib version 3.0

  • Operating system: Windows 10 64-bit
  • Matplotlib version:
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.6.6 Anaconda
  • Jupyter version (if applicable):
  • Other libraries: numpy (1.15.2)

using Anaconda

@jklymak
Copy link
Member

jklymak commented Oct 8, 2018

Can you make a complete example: http://sscce.org

@jklymak jklymak added the status: needs clarification Issues that need more information to resolve. label Oct 8, 2018
@bidhya
Copy link
Author

bidhya commented Oct 8, 2018

Example

Using Jupyter notebook

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

### Create a random array
a = np.random.rand(100, 100)
plt.imshow(a)
plt.colorbar();

##This second part will give error for the colorbar
### Create a boolean array
b = a > .5
print(b.dtype) # bool
plt.imshow(b) #This works
plt.colorbar() #TypeError even though it worked with older version of matplotlib/numpy

@bidhya bidhya closed this as completed Oct 8, 2018
@bidhya bidhya reopened this Oct 8, 2018
@bidhya
Copy link
Author

bidhya commented Oct 8, 2018

sorry closed it accidentally.

@ImportanceOfBeingErnest
Copy link
Member

Just to mention, plt.imshow(b.astype(int)) works fine. So if in doubt, using actual numbers is safer.
Still it looks like a regression.

@jklymak
Copy link
Member

jklymak commented Oct 8, 2018

Not sure if we support boolean arrays, but colorbar hasn't worked with this since at least 2.2.0. Suggest you cast to float or int.

@ImportanceOfBeingErnest ImportanceOfBeingErnest removed the status: needs clarification Issues that need more information to resolve. label Oct 8, 2018
@ImportanceOfBeingErnest ImportanceOfBeingErnest added this to the v3.0.x milestone Oct 8, 2018
@anntzer
Copy link
Contributor

anntzer commented Oct 8, 2018

fwiw this was already discussed in #8820.

@bidhya
Copy link
Author

bidhya commented Oct 8, 2018

Yes, true. Upon casting to float or int, the colorbar works properly. So I believe there is no plan to support colorbar for either boolean or categorical data.

@jklymak
Copy link
Member

jklymak commented Oct 8, 2018

Categorical would be a big change. Boolean would probably be easier, but frankly it’s probably better to defer to numpy who are not allowing Boolean math any longer.

@anntzer
Copy link
Contributor

anntzer commented Oct 10, 2018

Let's close the issue here as it was already discussed less than a year ago and we decided to defer to numpy's choice.
If you want to re-discuss this decision, the mailing list (with references to both discussions here) may be a better place to catch the core-devs' attention.

@anntzer anntzer closed this as completed Oct 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants