Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 607b4d6

Browse files
committed
add an explicit test about the 'c' kwarg for scatter
1 parent b68cc51 commit 607b4d6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,64 @@ def test_scatter_color(self):
17161716
with pytest.raises(ValueError):
17171717
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
17181718

1719+
# Parameters for *test_scatter_c_kwarg*. NB: assuming that
1720+
# the scatter plot will have 4 elements. The tuple scheme is:
1721+
# (*c* parameter case, exception regexp key or None if no exception)
1722+
params_scatter_c_kwarg = [
1723+
# Single letter-sequences
1724+
("rgby", None),
1725+
("rgb", "shape"),
1726+
("rgbrgb", "shape"),
1727+
(["rgby"], "conversion"),
1728+
# Special cases
1729+
("red", None),
1730+
("none", None),
1731+
(None, None),
1732+
(["r", "g", "b", "none"], None),
1733+
# Non-valid color spec (FWIW, 'jaune' means yellow in French)
1734+
("jaune", "conversion"),
1735+
(["jaune"], "conversion"), # wrong type before wrong size
1736+
(["jaune"]*4, "conversion"),
1737+
# Value-mapping like
1738+
([0.5]*3, None), # should emit a warning for user's eyes though
1739+
([0.5]*4, None), # NB: no warning as matching size allows mapping
1740+
([0.5]*5, "shape"),
1741+
# RGB values
1742+
([[1, 0, 0]], None),
1743+
([[1, 0, 0]]*3, "shape"),
1744+
([[1, 0, 0]]*4, None),
1745+
([[1, 0, 0]]*5, "shape"),
1746+
# RGBA values
1747+
([[1, 0, 0, 0.5]], None),
1748+
([[1, 0, 0, 0.5]]*3, "shape"),
1749+
([[1, 0, 0, 0.5]]*4, None),
1750+
([[1, 0, 0, 0.5]]*5, "shape"),
1751+
# Mix of valid color specs
1752+
([[1, 0, 0, 0.5]]*3 + [[1, 0, 0]], None),
1753+
([[1, 0, 0, 0.5], "red", "0.0"], "shape"),
1754+
([[1, 0, 0, 0.5], "red", "0.0", "C5"], None),
1755+
([[1, 0, 0, 0.5], "red", "0.0", "C5", [0, 1, 0]], "shape"),
1756+
# Mix of valid and non valid color specs
1757+
([[1, 0, 0, 0.5], "red", "jaune"], "conversion"),
1758+
([[1, 0, 0, 0.5], "red", "0.0", "jaune"], "conversion"),
1759+
([[1, 0, 0, 0.5], "red", "0.0", "C5", "jaune"], "conversion"),
1760+
]
1761+
1762+
@pytest.mark.parametrize('c_case, re_key', params_scatter_c_kwarg)
1763+
def test_scatter_c_kwarg(self, c_case, re_key):
1764+
# Additional checking of *c* (introduced in #11383).
1765+
REGEXP = {"shape": "^'c' kwarg has [0-9]+ elements", # shape mismatch
1766+
"conversion": "^'c' kwarg must either be valid", # bad vals
1767+
}
1768+
x = y = [0, 1, 2, 3]
1769+
fig, ax = plt.subplots()
1770+
1771+
if re_key is None:
1772+
ax.scatter(x, y, c=c_case, edgecolors="black")
1773+
else:
1774+
with pytest.raises(ValueError, match=REGEXP[re_key]):
1775+
ax.scatter(x, y, c=c_case, edgecolors="black")
1776+
17191777

17201778
def test_as_mpl_axes_api():
17211779
# tests the _as_mpl_axes api

0 commit comments

Comments
 (0)