@@ -4138,7 +4138,7 @@ def dopatch(xs, ys, **kwargs):
4138
4138
medians = medians , fliers = fliers , means = means )
4139
4139
4140
4140
@staticmethod
4141
- def _parse_scatter_color_args (c , edgecolors , kwargs , xshape , yshape ,
4141
+ def _parse_scatter_color_args (c , edgecolors , kwargs , xsize ,
4142
4142
get_next_color_func ):
4143
4143
"""
4144
4144
Helper function to process color related arguments of `.Axes.scatter`.
@@ -4168,8 +4168,8 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4168
4168
Additional kwargs. If these keys exist, we pop and process them:
4169
4169
'facecolors', 'facecolor', 'edgecolor', 'color'
4170
4170
Note: The dict is modified by this function.
4171
- xshape, yshape : tuple of int
4172
- The shape of the x and y arrays passed to `.Axes.scatter`.
4171
+ xsize : int
4172
+ The size of the x and y arrays passed to `.Axes.scatter`.
4173
4173
get_next_color_func : callable
4174
4174
A callable that returns a color. This color is used as facecolor
4175
4175
if no other color is provided.
@@ -4192,9 +4192,6 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4192
4192
The edgecolor specification.
4193
4193
4194
4194
"""
4195
- xsize = functools .reduce (operator .mul , xshape , 1 )
4196
- ysize = functools .reduce (operator .mul , yshape , 1 )
4197
-
4198
4195
facecolors = kwargs .pop ('facecolors' , None )
4199
4196
facecolors = kwargs .pop ('facecolor' , facecolors )
4200
4197
edgecolors = kwargs .pop ('edgecolor' , edgecolors )
@@ -4234,7 +4231,7 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4234
4231
# favor of mapping, not rgb or rgba.
4235
4232
# Convenience vars to track shape mismatch *and* conversion failures.
4236
4233
valid_shape = True # will be put to the test!
4237
- n_elem = - 1 # used only for ( some) exceptions
4234
+ csize = - 1 # Number of colors; used for some exceptions.
4238
4235
4239
4236
if (c_was_none or
4240
4237
kwcolor is not None or
@@ -4246,9 +4243,9 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4246
4243
else :
4247
4244
try : # First, does 'c' look suitable for value-mapping?
4248
4245
c_array = np .asanyarray (c , dtype = float )
4249
- n_elem = c_array .shape [ 0 ]
4250
- if c_array . shape in [ xshape , yshape ] :
4251
- c = np . ma . ravel (c_array )
4246
+ csize = c_array .size
4247
+ if csize == xsize :
4248
+ c = c_array . ravel ()
4252
4249
else :
4253
4250
if c_array .shape in ((3 ,), (4 ,)):
4254
4251
_log .warning (
@@ -4267,28 +4264,23 @@ def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4267
4264
if c_array is None :
4268
4265
try : # Then is 'c' acceptable as PathCollection facecolors?
4269
4266
colors = mcolors .to_rgba_array (c )
4270
- n_elem = colors .shape [0 ]
4271
- if colors . shape [ 0 ] not in (0 , 1 , xsize , ysize ):
4267
+ csize = colors .shape [0 ]
4268
+ if csize not in (0 , 1 , xsize ):
4272
4269
# NB: remember that a single color is also acceptable.
4273
4270
# Besides *colors* will be an empty array if c == 'none'.
4274
4271
valid_shape = False
4275
4272
raise ValueError
4276
4273
except ValueError :
4277
4274
if not valid_shape : # but at least one conversion succeeded.
4278
4275
raise ValueError (
4279
- "'c' argument has {nc} elements, which is not "
4280
- "acceptable for use with 'x' with size {xs}, "
4281
- "'y' with size {ys}."
4282
- .format (nc = n_elem , xs = xsize , ys = ysize )
4283
- )
4276
+ f"'c' argument has { csize } elements, which is "
4277
+ "inconsistent with 'x' and 'y' with size {xsize}." )
4284
4278
else :
4285
4279
# Both the mapping *and* the RGBA conversion failed: pretty
4286
4280
# severe failure => one may appreciate a verbose feedback.
4287
4281
raise ValueError (
4288
- "'c' argument must be a mpl color, a sequence of mpl "
4289
- "colors or a sequence of numbers, not {}."
4290
- .format (c ) # note: could be long depending on c
4291
- )
4282
+ f"'c' argument must be a mpl color, a sequence of mpl "
4283
+ "colors, or a sequence of numbers, not {c}." )
4292
4284
else :
4293
4285
colors = None # use cmap, norm after collection is created
4294
4286
return c , colors , edgecolors
@@ -4306,7 +4298,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
4306
4298
4307
4299
Parameters
4308
4300
----------
4309
- x, y : array_like, shape (n, )
4301
+ x, y : scalar or array_like, shape (n, )
4310
4302
The data positions.
4311
4303
4312
4304
s : scalar or array_like, shape (n, ), optional
@@ -4318,8 +4310,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
4318
4310
4319
4311
- A single color format string.
4320
4312
- A sequence of color specifications of length n.
4321
- - A sequence of n numbers to be mapped to colors using *cmap* and
4322
- *norm*.
4313
+ - A scalar or sequence of n numbers to be mapped to colors using
4314
+ *cmap* and * norm*.
4323
4315
- A 2-D array in which the rows are RGB or RGBA.
4324
4316
4325
4317
Note that *c* should not be a single numeric RGB or RGBA sequence
@@ -4408,7 +4400,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
4408
4400
plotted.
4409
4401
4410
4402
* Fundamentally, scatter works with 1-D arrays; *x*, *y*, *s*, and *c*
4411
- may be input as 2 -D arrays, but within scatter they will be
4403
+ may be input as N -D arrays, but within scatter they will be
4412
4404
flattened. The exception is *c*, which will be flattened only if its
4413
4405
size matches the size of *x* and *y*.
4414
4406
@@ -4421,7 +4413,6 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
4421
4413
4422
4414
# np.ma.ravel yields an ndarray, not a masked array,
4423
4415
# unless its argument is a masked array.
4424
- xshape , yshape = np .shape (x ), np .shape (y )
4425
4416
x = np .ma .ravel (x )
4426
4417
y = np .ma .ravel (y )
4427
4418
if x .size != y .size :
@@ -4430,11 +4421,13 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
4430
4421
if s is None :
4431
4422
s = (20 if rcParams ['_internal.classic_mode' ] else
4432
4423
rcParams ['lines.markersize' ] ** 2.0 )
4433
- s = np .ma .ravel (s ) # This doesn't have to match x, y in size.
4424
+ s = np .ma .ravel (s )
4425
+ if len (s ) not in (1 , x .size ):
4426
+ raise ValueError ("s must be a scalar, or the same size as x and y" )
4434
4427
4435
4428
c , colors , edgecolors = \
4436
4429
self ._parse_scatter_color_args (
4437
- c , edgecolors , kwargs , xshape , yshape ,
4430
+ c , edgecolors , kwargs , x . size ,
4438
4431
get_next_color_func = self ._get_patches_for_fill .get_next_color )
4439
4432
4440
4433
if plotnonfinite and colors is None :
0 commit comments