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)
---------------------------------------------------------------------------
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'
No error.
Bug summary
We are creating manual
ScalarMappableinstances 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, withScalarMappable.set_arraythere appears to be no assurances that the._Aattribute will support such comparison.Code for reproduction
Actual outcome
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