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

Skip to content

Commit bdd15a3

Browse files
committed
Further simplify scatter args parsing.
1 parent 20baf67 commit bdd15a3

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,29 +4229,26 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
42294229
or (isinstance(c, collections.abc.Iterable) and len(c) > 0
42304230
and isinstance(cbook.safe_first_element(c), str)))
42314231

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):
4232+
def invalid_shape_exception(csize, xsize):
42374233
return ValueError(
42384234
f"'c' argument has {csize} elements, which is inconsistent "
42394235
f"with 'x' and 'y' with size {xsize}.")
42404236

4241-
c_array = None
4242-
# Convenience var to track shape mismatch *and* conversion failures.
4243-
valid_shape = True # will be put to the test!
4237+
c_is_mapped = False # Unless proven otherwise below.
4238+
valid_shape = True # Unless proven otherwise below.
42444239
if not c_was_none and kwcolor is None and not c_is_string_or_strings:
42454240
try: # First, does 'c' look suitable for value-mapping?
4246-
c_array = np.asanyarray(c, dtype=float)
4241+
c = np.asanyarray(c, dtype=float)
42474242
except ValueError:
42484243
pass # Failed to convert to float array; must be color specs.
42494244
else:
4250-
csize = c_array.size
4251-
if csize == xsize:
4252-
c = c_array.ravel()
4245+
# If c can be either mapped values or a RGB(A) color, prefer
4246+
# the former if shapes match, the latter otherwise.
4247+
if c.size == xsize:
4248+
c = c.ravel()
4249+
c_is_mapped = True
42534250
else: # Wrong size; it must not be intended for mapping.
4254-
if c_array.shape in ((3,), (4,)):
4251+
if c.shape in ((3,), (4,)):
42554252
_log.warning(
42564253
"'c' argument looks like a single numeric RGB or "
42574254
"RGBA sequence, which should be avoided as value-"
@@ -4260,24 +4257,22 @@ def invalid_shape_exception(csize, nsize):
42604257
"with a single row if you really want to specify "
42614258
"the same RGB or RGBA value for all points.")
42624259
valid_shape = False
4263-
c_array = None
4264-
if c_array is None:
4260+
if not c_is_mapped:
42654261
try: # Is 'c' acceptable as PathCollection facecolors?
42664262
colors = mcolors.to_rgba_array(c)
42674263
except ValueError:
42684264
if not valid_shape:
4269-
raise invalid_shape_exception(csize, xsize)
4265+
raise invalid_shape_exception(c.size, xsize)
42704266
# Both the mapping *and* the RGBA conversion failed: pretty
42714267
# severe failure => one may appreciate a verbose feedback.
42724268
raise ValueError(
42734269
f"'c' argument must be a mpl color, a sequence of mpl "
42744270
f"colors, or a sequence of numbers, not {c}.")
42754271
else:
4276-
csize = colors.shape[0]
4277-
if csize not in (0, 1, xsize):
4272+
if len(colors) not in (0, 1, xsize):
42784273
# NB: remember that a single color is also acceptable.
42794274
# Besides *colors* will be an empty array if c == 'none'.
4280-
raise invalid_shape_exception(csize, xsize)
4275+
raise invalid_shape_exception(len(colors), xsize)
42814276
else:
42824277
colors = None # use cmap, norm after collection is created
42834278
return c, colors, edgecolors

0 commit comments

Comments
 (0)