Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: regression with ScalarMappable and .set_array and autoscale_None #20902

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

Closed
ryandvmartin opened this issue Aug 25, 2021 · 4 comments
Closed
Milestone

Comments

@ryandvmartin
Copy link

ryandvmartin commented Aug 25, 2021

Bug summary

We are creating manual ScalarMappable instances in some cases where colorbar creation is detached from the plotting event. Changed in #20511 an element-wise comparison of the color array is made, however, with ScalarMappable.set_array there appears to be no assurances that the ._A attribute will support such comparison.

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot as plt
norm = mpl.colors.LogNorm()
cmap = mpl.cm.get_cmap("viridis")
fig, ax = plt.subplots()
pts = mpl.cm.ScalarMappable(norm=norm, cmap=cmap)
pts.set_array([0.1, 0.9])
plt.colorbar(pts)

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-adec5b1bbfd1> in <module>
      6 pts = mpl.cm.ScalarMappable(norm=norm, cmap=cmap)
      7 pts.set_array([0.1, 0.9])
----> 8 plt.colorbar(pts)

~/rms-python/envs/mpl34/lib/python3.7/site-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw)
   2354                                'an image (with imshow) or a contour set ('
   2355                                'with contourf).')
-> 2356     ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kw)
   2357     return ret
   2358

~/rms-python/envs/mpl34/lib/python3.7/site-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   1174                              'panchor']
   1175         cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 1176         cb = cbar.Colorbar(cax, mappable, **cb_kw)
   1177
   1178         self.sca(current_ax)

~/rms-python/envs/mpl34/lib/python3.7/site-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kwargs)
   1170         # even if mappable.draw has not yet been called.
   1171         if mappable.get_array() is not None:
-> 1172             mappable.autoscale_None()
   1173
   1174         self.mappable = mappable

~/rms-python/envs/mpl34/lib/python3.7/site-packages/matplotlib/cm.py in autoscale_None(self)
    474         if self._A is None:
    475             raise TypeError('You must first set_array for mappable')
--> 476         self.norm.autoscale_None(self._A)
    477         self.changed()
    478

~/rms-python/envs/mpl34/lib/python3.7/site-packages/matplotlib/colors.py in autoscale_None(self, A)
   1541     def autoscale_None(self, A):
   1542         # docstring inherited.
-> 1543         super().autoscale_None(np.ma.array(A, mask=(A <= 0)))
   1544
   1545

TypeError: '<=' not supported between instances of 'list' and 'int'

Expected outcome

No error.

Operating system

Ubuntu

Matplotlib Version

3.4.3

Matplotlib Backend

agg

Python version

3.7.10

Jupyter version

NA

Other libraries

numpy 1.18.5

Installation

conda

Conda channel

conda-forge

@QuLogic QuLogic added this to the v3.4.4 milestone Aug 25, 2021
@anntzer
Copy link
Contributor

anntzer commented Aug 31, 2021

This should be fixed by #18870 in 3.5, I don't know if @QuLogic wants to backport that to 3.4.4?

@QuLogic
Copy link
Member

QuLogic commented Sep 2, 2021

Yes, I'm aware of that, but it seems to be a new feature. Also, it doesn't actually fix the problem, only the most directly accessible trigger of it.

@anntzer
Copy link
Contributor

anntzer commented Sep 3, 2021

What would be the other repros?

@QuLogic
Copy link
Member

QuLogic commented Nov 16, 2021

Calling it directly, really.

But we probably won't have a 3.4.4, so I'll close as fixed in 3.5.

@QuLogic QuLogic closed this as completed Nov 16, 2021
@QuLogic QuLogic modified the milestones: v3.4.4, v3.5.0 Nov 16, 2021
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

3 participants