8000 Reorganize scatter arguments parsing. · matplotlib/matplotlib@20baf67 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20baf67

Browse files
committed
Reorganize scatter arguments parsing.
I think this version is a bit easier to follow, by making the `try... except` tighter around the parts that can actually fail, and by avoiding the "raise a ValueError to catch it later to raise a different ValueError" pattern.
1 parent e3c9376 commit 20baf67

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,29 +4224,33 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
42244224
c = (facecolors if facecolors is not None
42254225
else "b" if rcParams['_internal.classic_mode']
42264226
else get_next_color_func())
4227-
4228-
# After this block, c_array will be None unless
4229-
# c is an array for mapping. The potential ambiguity
4230-
# with a sequence of 3 or 4 numbers is resolved in
4231-
# favor of mapping, not rgb or rgba.
4232-
# Convenience vars to track shape mismatch *and* conversion failures.
4227+
c_is_string_or_strings = (
4228+
isinstance(c, str)
4229+
or (isinstance(c, collections.abc.Iterable) and len(c) > 0
4230+
and isinstance(cbook.safe_first_element(c), str)))
4231+
4232+
# After this block, c_array will be None unless c is an array for
4233+
# mapping. The potential ambiguity with a sequence of 3 or 4 numbers
4234+
# is resolved in favor of mapping, not rgb or rgba.
4235+
4236+
def invalid_shape_exception(csize, nsize):
4237+
return ValueError(
4238+
f"'c' argument has {csize} elements, which is inconsistent "
4239+
f"with 'x' and 'y' with size {xsize}.")
4240+
4241+
c_array = None
4242+
# Convenience var to track shape mismatch *and* conversion failures.
42334243
valid_shape = True # will be put to the test!
4234-
csize = -1 # Number of colors; used for some exceptions.
4235-
4236-
if (c_was_none or
4237-
kwcolor is not None or
4238-
isinstance(c, str) or
4239-
(isinstance(c, collections.abc.Iterable) and
4240-
len(c) > 0 and
4241-
isinstance(cbook.safe_first_element(c), str))):
4242-
c_array = None
4243-
else:
4244+
if not c_was_none and kwcolor is None and not c_is_string_or_strings:
42444245
try: # First, does 'c' look suitable for value-mapping?
42454246
c_array = np.asanyarray(c, dtype=float)
4247+
except ValueError:
4248+
pass # Failed to convert to float array; must be color specs.
4249+
else:
42464250
csize = c_array.size
42474251
if csize == xsize:
42484252
c = c_array.ravel()
4249-
else:
4253+
else: # Wrong size; it must not be intended for mapping.
42504254
if c_array.shape in ((3,), (4,)):
42514255
_log.warning(
42524256
"'c' argument looks like a single numeric RGB or "
@@ -4255,32 +4259,25 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
42554259
"matches with 'x' & 'y'. Please use a 2-D array "
42564260
"with a single row if you really want to specify "
42574261
"the same RGB or RGBA value for all points.")
4258-
# Wrong size; it must not be intended for mapping.
42594262
valid_shape = False
42604263
c_array = None
4261-
except ValueError:
4262-
# Failed to make a floating-point array; c must be color specs.
4263-
c_array = None
42644264
if c_array is None:
4265-
try: # Then is 'c' acceptable as PathCollection facecolors?
4265+
try: # Is 'c' acceptable as PathCollection facecolors?
42664266
colors = mcolors.to_rgba_array(c)
4267+
except ValueError:
4268+
if not valid_shape:
4269+
raise invalid_shape_exception(csize, xsize)
4270+
# Both the mapping *and* the RGBA conversion failed: pretty
4271+
# severe failure => one may appreciate a verbose feedback.
4272+
raise ValueError(
4273+
f"'c' argument must be a mpl color, a sequence of mpl "
4274+
f"colors, or a sequence of numbers, not {c}.")
4275+
else:
42674276
csize = colors.shape[0]
42684277
if csize not in (0, 1, xsize):
42694278
# NB: remember that a single color is also acceptable.
42704279
# Besides *colors* will be an empty array if c == 'none'.
4271-
valid_shape = False
4272-
raise ValueError
4273-
except ValueError:
4274-
if not valid_shape: # but at least one conversion succeeded.
4275-
raise ValueError(
4276-
f"'c' argument has {csize} elements, which is "
4277-
"inconsistent with 'x' and 'y' with size {xsize}.")
4278-
else:
4279-
# Both the mapping *and* the RGBA conversion failed: pretty
4280-
# severe failure => one may appreciate a verbose feedback.
4281-
raise ValueError(
4282-
f"'c' argument must be a mpl color, a sequence of mpl "
4283-
"colors, or a sequence of numbers, not {c}.")
4280+
raise invalid_shape_exception(csize, xsize)
42844281
else:
42854282
colors = None # use cmap, norm after collection is created
42864283
return c, colors, edgecolors

0 commit comments

Comments
 (0)
0