@@ -3792,7 +3792,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
3792
3792
Note that *c* should not be a single numeric RGB or RGBA sequence
3793
3793
because that is indistinguishable from an array of values to be
3794
3794
colormapped. If you want to specify the same RGB or RGBA value for
3795
- all points, use a 2-D array with a single row.
3795
+ all points, use a 2-D array with a single row. Otherwise, value-
3796
+ matching will have precedence in case of a size matching with *x*
3797
+ and *y*.
3796
3798
3797
3799
marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'
3798
3800
The marker style. *marker* can be either an instance of the class
@@ -3925,29 +3927,58 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
3925
3927
# c is an array for mapping. The potential ambiguity
3926
3928
# with a sequence of 3 or 4 numbers is resolved in
3927
3929
# favor of mapping, not rgb or rgba.
3930
+
3931
+ # Convenience vars to track shape mismatch *and* conversion failures.
3932
+ valid_shape = True # will be put to the test!
3933
+ n_elem = 0 # used only for (some) exceptions
3934
+
3928
3935
if c_none or co is not None :
3929
3936
c_array = None
3930
3937
else :
3931
- try :
3938
+ try : # First, does 'c' look suitable for value-mapping?
3932
3939
c_array = np .asanyarray (c , dtype = float )
3940
+ n_elem = c_array .shape [0 ]
3933
3941
if c_array .shape in xy_shape :
3934
3942
c = np .ma .ravel (c_array )
3935
3943
else :
3944
+ if c_array .shape in ((3 ,), (4 ,)):
3945
+ _log .warning (
3946
+ "'c' kwarg looks like a **single** numeric RGB or "
3947
+ "RGBA sequence, which should be avoided as value-"
3948
+ "mapping will have precedence in case its length "
3949
+ "matches with 'x' & 'y'. Please use a 2-D array "
3950
+ "with a single row if you really want to specify "
3951
+ "the same RGB or RGBA value for all points." )
3936
3952
# Wrong size; it must not be intended for mapping.
3953
+ valid_shape = False
3937
3954
c_array = None
3938
3955
except ValueError :
3939
3956
# Failed to make a floating-point array; c must be color specs.
3940
3957
c_array = None
3941
3958
3942
3959
if c_array is None :
3943
- try :
3944
- # must be acceptable as PathCollection facecolors
3960
+ try : # Then is 'c' acceptable as PathCollection facecolors?
3945
3961
colors = mcolors .to_rgba_array (c )
3962
+ n_elem = colors .shape [0 ]
3963
+ if colors .shape [0 ] not in (1 , x .size , y .size ):
3964
+ # NB: remember that a single color is also acceptable.
3965
+ valid_shape = False
3966
+ raise ValueError
3946
3967
except ValueError :
3947
- # c not acceptable as PathCollection facecolor
3948
- raise ValueError ("c of shape {} not acceptable as a color "
3949
- "sequence for x with size {}, y with size {}"
3950
- .format (c .shape , x .size , y .size ))
3968
+ if not valid_shape : # but at least one conversion succeeded.
3969
+ raise ValueError (
3970
+ "'c' kwarg has {nc} elements, which is not acceptable "
3971
+ "for use with 'x' with size {xs}, 'y' with size {ys}."
3972
+ .format (nc = n_elem , xs = x .size , ys = y .size )
3973
+ )
3974
+ # Both the mapping *and* the RGBA conversion failed: pretty
3975
+ # severe failure => one may appreciate a verbose feedback.
3976
+ raise ValueError (
3977
+ "'c' kwarg must either be valid as mpl color(s) or "
3978
+ "as numbers to be mapped to colors. "
3979
+ "Here c = {}." # <- beware, could be long depending on c.
3980
+ .format (c )
3981
+ )
3951
3982
else :
3952
3983
colors = None # use cmap, norm after collection is created
3953
3984
0 commit comments