FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: regression with ScalarMappable and `.set_array` and `autoscale_None` · Issue #20902 · matplotlib/matplotlib · GitHub

Repository navigation

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

Description

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

Activity

  1. added this to the v3.4.4 milestone on Aug 25, 2021
  2. anntzer commented on Aug 31, 2021

    Contributor

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

  3. QuLogic commented on Sep 2, 2021

    Member

    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.

  4. anntzer commented on Sep 3, 2021

    Contributor

    What would be the other repros?

  5. QuLogic commented on Nov 16, 2021

    Member

    Calling it directly, really.

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

  6. modified the milestones: v3.4.4, v3.5.0 on Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL