8000 Fixups from review · matplotlib/matplotlib@c9d0741 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d0741

Browse files
committed
Fixups from review
Fixup and clarification in colors; more tests Tweak whats_new
1 parent 821617b commit c9d0741

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

doc/users/next_whats_new/alpha_array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Transparency (alpha) can be set as an array in collections
22
----------------------------------------------------------
33
Previously, the alpha value controlling tranparency in collections could be
4-
specified only as a scalar which was applied to all elements in the collection.
4+
specified only as a scalar applied to all elements in the collection.
55
For example, all the markers in a `~.Axes.scatter` plot, or all the
66
quadrilaterals in a `~.Axes.pcolormesh` plot, would have the same alpha value.
77

lib/matplotlib/artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ def _set_alpha_for_array(self, alpha):
982982
Artist.set_alpha(self, alpha)
983983
return
984984
alpha = np.asarray(alpha)
985-
if not (alpha.min() >= 0 and alpha.max() <= 1):
985+
if not (0 <= alpha.min() and alpha.max() <= 1):
986986
raise ValueError('alpha must be between 0 and 1, inclusive, '
987987
f'but min is {alpha.min()}, max is {alpha.max()}')
988988
self._alpha = alpha

lib/matplotlib/collections.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,9 @@ def set_alpha(self, alpha):
837837
838838
Parameters
839839
----------
840-
alpha: float or array of float
840+
alpha: float or array of float or None
841841
If not None, *alpha* values must be between 0 and 1, inclusive.
842-
If an array is provided, it's length must match the number of
842+
If an array is provided, its length must match the number of
843843
elements in the collection. Masked values and nans are not
844844
supported.
845845
"""
@@ -867,13 +867,13 @@ def update_scalarmappable(self):
867867
return
868868
if np.iterable(self._alpha):
869869
if self._alpha.size != self._A.size:
870-
# This can occur with the deprecated behavior of 'flat'
871-
# pcolormesh shading. If we bring the current change in
872-
# before that deprecated behavior is removed, we need to
873-
# add the explanation to the message below.
874870
raise ValueError(f'Data array shape, {self._A.shape} '
875871
'is incompatible with alpha array shape, '
876-
f'{self._alpha.shape}.')
872+
f'{self._alpha.shape}. '
873+
'This can occur with the deprecated '
874+
'behavior of the "flat" shading option, '
875+
'in which a row and/or column of the data '
876+
'array is dropped.')
877877
# pcolormesh, scatter, maybe others flatten their _A
878878
self._alpha = self._alpha.reshape(self._A.shape)
879879

lib/matplotlib/colors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,20 +615,20 @@ def __call__(self, X, alpha=None, bytes=False):
615615
if alpha is not None:
616616
if np.iterable(alpha):
617617
alpha = np.asarray(alpha)
618-
if not (alpha.shape == xa.shape):
619-
raise ValueError("alpha is array-like but it's shape"
618+
if alpha.shape != xa.shape:
619+
raise ValueError("alpha is array-like but its shape"
620620
" %s doesn't match that of X %s" %
621621
(alpha.shape, xa.shape))
622-
623622
alpha = np.clip(alpha, 0, 1)
624623
if bytes:
625624
alpha = (alpha * 255).astype(np.uint8)
626625
rgba[..., -1] = alpha
627626

628-
if (lut[-1] == 0).all() and mask_bad is not None:
629-
if mask_bad.shape == xa.shape:
627+
# If the "bad" color is all zeros, then ignore alpha input.
628+
if (lut[-1] == 0).all() and np.any(mask_bad):
629+
if np.iterable(mask_bad) and mask_bad.shape == xa.shape:
630630
rgba[mask_bad] = (0, 0, 0, 0)
631-
elif mask_bad:
631+
else:
632632
rgba[..., :] = (0, 0, 0, 0)
633633

634634
if not np.iterable(X):

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def set_alpha(self, alpha):
274274
275275
Parameters
276276
----------
277-
alpha : float or 2-d array or None
277+
alpha : float or 2D array-like or None
278278
"""
279279
martist.Artist._set_alpha_for_array(self, alpha)
280280
if np.ndim(alpha) not in (0, 2):

lib/matplotlib/tests/test_colors.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,25 @@ def test_get_under_over_bad():
11891189

11901190
def test_colormap_alpha_array():
11911191
cmap = plt.get_cmap('viridis')
1192+
vals = [-1, 0.5, 2] # under, valid, over
11921193
with pytest.raises(ValueError, match="alpha is array-like but"):
1193-
cmap([1, 2, 3], alpha=[1, 1, 1, 1])
1194-
alpha = [0.1, 0.2, 0.3]
1195-
c = cmap([1, 2, 3], alpha=alpha)
1194+
cmap(vals, alpha=[1, 1, 1, 1])
1195+
alpha = np.array([0.1, 0.2, 0.3])
1196+
c = cmap(vals, alpha=alpha)
11961197
assert_array_equal(c[:, -1], alpha)
1198+
c = cmap(vals, alpha=alpha, bytes=True)
1199+
assert_array_equal(c[:, -1], (alpha * 255).astype(np.uint8))
1200+
1201+
1202+
def test_colormap_bad_data_with_alpha():
1203+
cmap = plt.get_cmap('viridis')
1204+
c = cmap(np.nan, alpha=0.5)
1205+
assert c == (0, 0, 0, 0)
1206+
c = cmap([0.5, np.nan], alpha=0.5)
1207+
assert_array_equal(c[1], (0, 0, 0, 0))
1208+
c = cmap([0.5, np.nan], alpha=[0.1, 0.2])
1209+
assert_array_equal(c[1], (0, 0, 0, 0))
1210+
c = cmap([[np.nan, 0.5], [0, 0]], alpha=0.5)
1211+
assert_array_equal(c[0, 0], (0, 0, 0, 0))
1212+
c = cmap([[np.nan, 0.5], [0, 0]], alpha=np.full((2, 2), 0.5))
1213+
assert_array_equal(c[0, 0], (0, 0, 0, 0))

0 commit comments

Comments
 (0)
0