Add and use Artist.set_alpha_or_array · matplotlib/matplotlib@441c4a6 · GitHub
Skip to content

Commit 441c4a6

Browse files
committed
Add and use Artist.set_alpha_or_array
1 parent 9a088ab commit 441c4a6

3 files changed

Lines changed: 39 additions & 18 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 21 additions & 0 deletions

lib/matplotlib/collections.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -832,29 +832,15 @@ def set_edgecolor(self, c):
832832
self._set_edgecolor(c)
833833

834834
def set_alpha(self, alpha):
835-
"""
836-
Set the alpha tranparencies of the collection. *alpha* must be
837-
a float, a sequence of floats matching an array to be
838-
colormapped, or *None*.
839-
840-
ACCEPTS: float or None
841-
"""
842-
if alpha is not None:
843-
if np.iterable(alpha):
844-
alpha = np.asarray(alpha, dtype=float)
845-
else:
846-
try:
847-
alpha = float(alpha)
848-
except TypeError:
849-
raise TypeError('alpha must be a float,'
850-
' sequence of float, or None')
851-
super().set_alpha(alpha)
835+
artist.Artist.set_alpha_or_array(self, alpha)
852836
self._update_dict['array'] = True
853837
if alpha is not None and isinstance(alpha, np.ndarray):
854838
return
855839
self._set_facecolor(self._original_facecolor)
856840
self._set_edgecolor(self._original_edgecolor)
857841

842+
set_alpha.__doc__ = artist.Artist.set_alpha_or_array.__doc__
843+
858844
def get_linewidth(self):
859845
return self._linewidths
860846

@@ -865,11 +851,24 @@ def update_scalarmappable(self):
865851
"""Update colors from the scalar mappable array, if it is not None."""
866852
if self._A is None:
867853
return
868-
# QuadMesh can map 2d arrays
854+
# QuadMesh can map 2d arrays (but pcolormesh supplies 1d array)
869855
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
870856
raise ValueError('Collections can only map rank 1 arrays')
871857
if not self._check_update("array"):
872858
return
859+
if np.iterable(self._alpha) and isinstance(self, QuadMesh):
860+
if self._alpha.size != self._A.size:
861+
# This can occur with the deprecated behavior of 'flat'
862+
# pcolormesh shading. If we bring the current change in
863+
# before that deprecated behavior is removed, we need to
864+
# add the explanation to the message below.
865+
raise ValueError(f'Data array shape, {self._A.shape} '
866+
'is incompatible with alpha array shape, '
867+
f'{self._alpha.shape}.')
868+
# Handle the unfortunate behavior of pcolormesh in flattening
869+
# its color array here rather than in to_rgba.
870+
self._alpha = self._alpha.reshape(self._A.shape)
871+
873872
if self._is_filled:
874873
self._facecolors = self.to_rgba(self._A, self._alpha)
875874
elif self._is_stroked:

lib/matplotlib/colors.py

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)