@@ -4224,29 +4224,33 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
4224
4224
c = (facecolors if facecolors is not None
4225
4225
else "b" if rcParams ['_internal.classic_mode' ]
4226
4226
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.
4233
4243
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 :
4244
4245
try : # First, does 'c' look suitable for value-mapping?
4245
4246
c_array = np .asanyarray (c , dtype = float )
4247
+ except ValueError :
4248
+ pass # Failed to convert to float array; must be color specs.
4249
+ else :
4246
4250
csize = c_array .size
4247
4251
if csize == xsize :
4248
4252
c = c_array .ravel ()
4249
- else :
4253
+ else : # Wrong size; it must not be intended for mapping.
4250
4254
if c_array .shape in ((3 ,), (4 ,)):
4251
4255
_log .warning (
4252
4256
"'c' argument looks like a single numeric RGB or "
@@ -4255,32 +4259,25 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xsize,
4255
4259
"matches with 'x' & 'y'. Please use a 2-D array "
4256
4260
"with a single row if you really want to specify "
4257
4261
"the same RGB or RGBA value for all points." )
4258
- # Wrong size; it must not be intended for mapping.
4259
4262
valid_shape = False
4260
4263
c_array = None
4261
- except ValueError :
4262
- # Failed to make a floating-point array; c must be color specs.
4263
- c_array = None
4264
4264
if c_array is None :
4265
- try : # Then is 'c' acceptable as PathCollection facecolors?
4265
+ try : # Is 'c' acceptable as PathCollection facecolors?
4266
4266
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 :
4267
4276
csize = colors .shape [0 ]
4268
4277
if csize not in (0 , 1 , xsize ):
4269
4278
# NB: remember that a single color is also acceptable.
4270
4279
# 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 )
4284
4281
else :
4285
4282
colors = None # use cmap, norm after collection is created
4286
4283
return c , colors , edgecolors
0 commit comments