Skip to content

Commit eb38d25

Browse files
committed
Improve error when passing 0d array to scatter().
With `scatter([1, 2], [3, 4], c=np.array(.5))`... previously: <elided> File ".../matplotlib/colors.py", line 338, in to_rgba_array if len(c) == 0: TypeError: len() of unsized object now <elided> File ".../matplotlib/axes/_axes.py", line 4249, in _parse_scatter_color_args raise invalid_shape_exception(c.size, xsize) ValueError: 'c' argument has 1 elements, which is inconsistent with 'x' and 'y' with size 2. Essentially the problem is that np.array(.5) passes an `isinstance(..., Iterable)` check (which only checks the type) but np.iterable() more correctly returns False because it actually tries to iter on it.
1 parent cf6c569 commit eb38d25

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

lib/matplotlib/axes/_axes.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import collections.abc
21
import functools
32
import itertools
43
import logging
@@ -4210,7 +4209,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
42104209
else get_next_color_func())
42114210
c_is_string_or_strings = (
42124211
isinstance(c, str)
4213-
or (isinstance(c, collections.abc.Iterable) and len(c) > 0
4212+
or (np.iterable(c) and len(c) > 0
42144213
and isinstance(cbook.safe_first_element(c), str)))
42154214

42164215
def invalid_shape_exception(csize, xsize):
@@ -4244,7 +4243,7 @@ def invalid_shape_exception(csize, xsize):
42444243
if not c_is_mapped:
42454244
try: # Is 'c' acceptable as PathCollection facecolors?
42464245
colors = mcolors.to_rgba_array(c)
4247-
except ValueError:
4246+
except (TypeError, ValueError):
42484247
if not valid_shape:
42494248
raise invalid_shape_exception(c.size, xsize)
42504249
# Both the mapping *and* the RGBA conversion failed: pretty

0 commit comments

Comments
 (0)