Skip to content

Commit 9c24406

Browse files
committed
More validation and testing changes; WIP
1 parent 13995a4 commit 9c24406

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

lib/matplotlib/colors.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,8 @@ def __call__(self, X, alpha=None, bytes=False):
613613
lut.take(xa, axis=0, mode='clip', out=rgba)
614614

615615
if alpha is not None:
616-
if not np.iterable(alpha):
617-
if alpha < 0 or alpha > 1:
618-
raise ValueError("alpha must be in [0, 1];"
619-
" found %s" % alpha)
620-
else:
616+
if np.iterable(alpha):
621617
alpha = np.asarray(alpha)
622-
if alpha.min() < 0 or alpha.max() > 1:
623-
raise ValueError("alpha must be in [0, 1];"
624-
" found min %s and max %s" %
625-
(alpha.min(), alpha.max()))
626618
if not (alpha.shape == xa.shape):
627619
raise ValueError("alpha is array-like but it's shape"
628620
" %s doesn't match that of X %s" %

lib/matplotlib/tests/test_artist.py

+26
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,29 @@ def test_artist_inspector_get_aliases():
277277
ai = martist.ArtistInspector(mlines.Line2D)
278278
aliases = ai.get_aliases()
279279
assert aliases["linewidth"] == {"lw"}
280+
281+
282+
def test_set_alpha():
283+
art = martist.Artist()
284+
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
285+
art.set_alpha('string')
286+
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
287+
art.set_alpha([1, 2, 3])
288+
with pytest.raises(ValueError, match="outside 0-1 range"):
289+
art.set_alpha(1.1)
290+
with pytest.raises(ValueError, match="outside 0-1 range"):
291+
art.set_alpha(np.nan)
292+
293+
294+
def test_set_alpha_for_array():
295+
art = martist.Artist()
296+
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
297+
art._set_alpha_for_array('string')
298+
with pytest.raises(ValueError, match="outside 0-1 range"):
299+
art._set_alpha_for_array(1.1)
300+
with pytest.raises(ValueError, match="outside 0-1 range"):
301+
art._set_alpha_for_array(np.nan)
302+
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
303+
art._set_alpha_for_array([0.5, 1.1])
304+
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
305+
art._set_alpha_for_array([0.5, np.nan])

lib/matplotlib/tests/test_collections.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -714,25 +714,29 @@ def test_quadmesh_alpha_array():
714714

715715

716716
def test_alpha_validation():
717+
# Most of the relevant testing is in test_artist and test_colors.
717718
fig, ax = plt.subplots()
718719
pc = ax.pcolormesh(np.arange(12).reshape((3, 4)))
719-
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
720-
pc.set_alpha('string')
721-
with pytest.raises(ValueError, match="outside 0-1 range"):
722-
pc.set_alpha(1.1)
723-
with pytest.raises(ValueError, match="outside 0-1 range"):
724-
pc.set_alpha(np.nan)
725-
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
726-
pc.set_alpha([0.5, 1.1])
727-
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
728-
pc.set_alpha([0.5, np.nan])
720+
# with pytest.raises(TypeError, match='^alpha must be numeric or None'):
721+
# pc.set_alpha('string')
722+
# with pytest.raises(ValueError, match="outside 0-1 range"):
723+
# pc.set_alpha(1.1)
724+
# with pytest.raises(ValueError, match="outside 0-1 range"):
725+
# pc.set_alpha(np.nan)
726+
# with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
727+
# pc.set_alpha([0.5, 1.1])
728+
# with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
729+
# pc.set_alpha([0.5, np.nan])
729730
with pytest.raises(ValueError, match="^Data array shape"):
730731
pc.set_alpha([0.5, 0.6])
731732
pc.update_scalarmappable()
732-
ax.cla()
733-
sc = ax.scatter([1, 2], [1, 2], color=['r', 'g'])
734-
with pytest.raises(ValueError, match="^The number of colors must match"):
735-
sc.set_alpha([0.5, 0.5, 0.5])
733+
# sc = ax.scatter([1, 2], [1, 2], color=['r', 'g'])
734+
# with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
735+
# sc.set_alpha([0.5, 1.1])
736+
# with pytest.raises(ValueError, match="outside 0-1 range"):
737+
# sc.set_alpha(1.1)
738+
# with pytest.raises(ValueError, match="The number of colors must match"):
739+
# sc.set_alpha([[1, 1, 1]])
736740

737741

738742
def test_legend_inverse_size_label_relationship():

lib/matplotlib/tests/test_colors.py

+19
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,16 @@ def test_to_rgba_array_single_str():
10721072
array = mcolors.to_rgba_array("rgb")
10731073

10741074

1075+
def test_to_rgba_array_alpha_array():
1076+
with pytest.raises(ValueError, match="The number of colors must match"):
1077+
mcolors.to_rgba_array(np.ones((5, 3), float), alpha=np.ones((2,)))
1078+
alpha = [0.5, 0.6]
1079+
c = mcolors.to_rgba_array(np.ones((2, 3), float), alpha=alpha)
1080+
assert_array_equal(c[:, 3], alpha)
1081+
c = mcolors.to_rgba_array(['r', 'g'], alpha=alpha)
1082+
assert_array_equal(c[:, 3], alpha)
1083+
1084+
10751085
def test_failed_conversions():
10761086
with pytest.raises(ValueError):
10771087
mcolors.to_rgba('5')
@@ -1175,3 +1185,12 @@ def test_get_under_over_bad():
11751185
assert_array_equal(cmap.get_under(), cmap(-np.inf))
11761186
assert_array_equal(cmap.get_over(), cmap(np.inf))
11771187
assert_array_equal(cmap.get_bad(), cmap(np.nan))
1188+
1189+
1190+
def test_colormap_alpha_array():
1191+
cmap = plt.get_cmap('viridis')
1192+
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)
1196+
assert_array_equal(c[:, -1], alpha)

lib/matplotlib/tests/test_image.py

+5
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,11 @@ def test_image_array_alpha(fig_test, fig_ref):
10911091
ax.imshow(rgba, interpolation='nearest')
10921092

10931093

1094+
def test_image_array_alpha_validation():
1095+
with pytest.raises(TypeError, match="alpha must be a float, two-d"):
1096+
plt.imshow(np.zeros((2, 2)), alpha=[1, 1])
1097+
1098+
10941099
@pytest.mark.style('mpl20')
10951100
def test_exact_vmin():
10961101
cmap = copy(plt.cm.get_cmap("autumn_r"))

0 commit comments

Comments
 (0)