@@ -4229,29 +4229,26 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
4229
4229
or (isinstance (c , collections .abc .Iterable ) and len (c ) > 0
4230
4230
and isinstance (cbook .safe_first_element (c ), str )))
4231
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 ):
4232
+ def invalid_shape_exception (csize , xsize ):
4237
4233
return ValueError (
4238
4234
f"'c' argument has { csize } elements, which is inconsistent "
4239
4235
f"with 'x' and 'y' with size { xsize } ." )
4240
4236
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.
4244
4239
if not c_was_none and kwcolor is None and not c_is_string_or_strings :
4245
4240
try : # First, does 'c' look suitable for value-mapping?
4246
- c_array = np .asanyarray (c , dtype = float )
4241
+ c = np .asanyarray (c , dtype = float )
4247
4242
except ValueError :
4248
4243
pass # Failed to convert to float array; must be color specs.
4249
4244
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
4253
4250
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 ,)):
4255
4252
_log .warning (
4256
4253
"'c' argument looks like a single numeric RGB or "
4257
4254
"RGBA sequence, which should be avoided as value-"
@@ -4260,24 +4257,22 @@ def invalid_shape_exception(csize, nsize):
4260
4257
"with a single row if you really want to specify "
4261
4258
"the same RGB or RGBA value for all points." )
4262
4259
valid_shape = False
4263
- c_array = None
4264
- if c_array is None :
4260
+ if not c_is_mapped :
4265
4261
try : # Is 'c' acceptable as PathCollection facecolors?
4266
4262
colors = mcolors .to_rgba_array (c )
4267
4263
except ValueError :
4268
4264
if not valid_shape :
4269
- raise invalid_shape_exception (csize , xsize )
4265
+ raise invalid_shape_exception (c . size , xsize )
4270
4266
# Both the mapping *and* the RGBA conversion failed: pretty
4271
4267
# severe failure => one may appreciate a verbose feedback.
4272
4268
raise ValueError (
4273
4269
f"'c' argument must be a mpl color, a sequence of mpl "
4274
4270
f"colors, or a sequence of numbers, not { c } ." )
4275
4271
else :
4276
- csize = colors .shape [0 ]
4277
- if csize not in (0 , 1 , xsize ):
4272
+ if len (colors ) not in (0 , 1 , xsize ):
4278
4273
# NB: remember that a single color is also acceptable.
4279
4274
# 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 )
4281
4276
else :
4282
4277
colors = None # use cmap, norm after collection is created
4283
4278
return c , colors , edgecolors
0 commit comments