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

Skip to content

Commit d28c02e

Browse files
committed
MNT: trap inappropriate use of color kwarg in scatter; closes #6266
This slightly modifies and extends the special-casing of color-related kwargs that would otherwise be passed in to a Collection instance. Attempts to use 'color' in place of the 'c' kwarg for color-mapping in scatter are now trapped with a ValueError in most cases. There are still cases that are impossible to trap: a sequence of 3 or 4 floats between 0 and 1 could be either a single color spec or a sequence of values to be color-mapped.
1 parent b49bcf4 commit d28c02e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,19 +3824,20 @@ def scatter(self, x, y, s=None, c=None, marker='o', cmap=None, norm=None,
38243824
# Process **kwargs to handle aliases, conflicts with explicit kwargs:
38253825

38263826
facecolors = None
3827-
ec = kwargs.pop('edgecolor', None)
3828-
if ec is not None:
3829-
edgecolors = ec
3830-
fc = kwargs.pop('facecolor', None)
3831-
if fc is not None:
3832-
facecolors = fc
3827+
edgecolors = kwargs.pop('edgecolor', edgecolors)
38333828
fc = kwargs.pop('facecolors', None)
3829+
fc = kwargs.pop('facecolor', fc)
38343830
if fc is not None:
38353831
facecolors = fc
3836-
# 'color' should be deprecated in scatter, or clearly defined;
3837-
# since it isn't, I am giving it low priority.
38383832
co = kwargs.pop('color', None)
38393833
if co is not None:
3834+
try:
3835+
mcolors.colorConverter.to_rgba_array(co)
3836+
except ValueError:
3837+
raise ValueError("'color' kwarg must be an mpl color"
3838+
" spec or sequence of color specs.\n"
3839+
"For a sequence of values to be"
3840+
" color-mapped, use the 'c' kwarg instead.")
38403841
if edgecolors is None:
38413842
edgecolors = co
38423843
if facecolors is None:

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,13 @@ def test_scatter_2D():
12851285
fig, ax = plt.subplots()
12861286
ax.scatter(x, y, c=z, s=200, edgecolors='face')
12871287

1288+
@cleanup
1289+
def test_scatter_color():
1290+
# Try to catch cases where 'c' kwarg should have been used.
1291+
assert_raises(ValueError, plt.scatter, [1, 2], [1, 2],
1292+
color=[0.1, 0.2])
1293+
assert_raises(ValueError, plt.scatter, [1, 2, 3], [1, 2, 3],
1294+
color=[1, 2, 3])
12881295

12891296
@cleanup
12901297
def test_as_mpl_axes_api():

0 commit comments

Comments
 (0)