8000 Merge pull request #17499 from tacaswell/fix_scatter_singlecolor · matplotlib/matplotlib@346f6f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 346f6f7

Browse files
authored
Merge pull request #17499 from tacaswell/fix_scatter_singlecolor
Fix scatter singlecolor
2 parents 6ed975c + 1a56e12 commit 346f6f7

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,19 +4281,26 @@ def invalid_shape_exception(csize, xsize):
42814281
except ValueError:
42824282
pass # Failed to convert to float array; must be color specs.
42834283
else:
4284+
# handle the documented special case of a 2D array with 1
4285+
# row which as RGB(A) to broadcast.
4286+
if c.shape == (1, 4) or c.shape == (1, 3):
4287+
c_is_mapped = False
4288+
if c.size != xsize:
4289+
valid_shape = False
42844290
# If c can be either mapped values or a RGB(A) color, prefer
42854291
# the former if shapes match, the latter otherwise.
4286-
if c.size == xsize:
4292+
elif c.size == xsize:
42874293
c = c.ravel()
42884294
c_is_mapped = True
42894295
else: # Wrong size; it must not be intended for mapping.
42904296
if c.shape in ((3,), (4,)):
42914297
_log.warning(
4292-
"'c' argument looks like a single numeric RGB or "
4298+
"*c* argument looks like a single numeric RGB or "
42934299
"RGBA sequence, which should be avoided as value-"
42944300
"mapping will have precedence in case its length "
4295-
"matches with 'x' & 'y'. Please use a 2-D array "
4296-
"with a single row if you really want to specify "
4301+
"matches with *x* & *y*. Please use the *color* "
4302+
"keyword-argument or provide a 2-D array "
4303+
"with a single row if you intend to specify "
42974304
"the same RGB or RGBA value for all points.")
42984305
valid_shape = False
42994306
if not c_is_mapped:
@@ -4340,14 +4347,14 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43404347
The marker size in points**2.
43414348
Default is ``rcParams['lines.markersize'] ** 2``.
43424349
4343-
c : color or list of colors or array-like, optional
4344-
The marker color. Possible values:
4350+
c : array-like or list of colors or color, optional
4351+
The marker colors. Possible values:
43454352
4346-
- A single color format string.
4347-
- A sequence of colors of length n.
43484353
- A scalar or sequence of n numbers to be mapped to colors using
43494354
*cmap* and *norm*.
43504355
- A 2-D array in which the rows are RGB or RGBA.
4356+
- A sequence of colors of length n.
4357+
- A single color format string.
43514358
43524359
Note that *c* should not be a single numeric RGB or RGBA sequence
43534360
because that is indistinguishable from an array of values to be
@@ -4356,9 +4363,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43564363
matching will have precedence in case of a size matching with *x*
43574364
and *y*.
43584365
4359-
Defaults to ``None``. In that case the marker color is determined
4360-
by the value of ``color``, ``facecolor`` or ``facecolors``. In case
4361-
those are not specified or ``None``, the marker color is determined
4366+
If you wish to specify a single color for all points
4367+
prefer the *color* keyword argument.
4368+
4369+
Defaults to `None`. In that case the marker color is determined
4370+
by the value of *color*, *facecolor* or *facecolors*. In case
4371+
those are not specified or `None`, the marker color is determined
43624372
by the next color of the ``Axes``' current "shape and fill" color
43634373
cycle. This cycle defaults to :rc:`axes.prop_cycle`.
43644374

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,22 @@ def get_next_color():
19681968
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
19691969
get_next_color_func=get_next_color)
19701970

1971+
@pytest.mark.style('default')
1972+
@check_figures_equal(extensions=["png"])
1973+
def test_scatter_single_color_c(self, fig_test, fig_ref):
1974+
rgb = [[1, 0.5, 0.05]]
1975+
rgba = [[1, 0.5, 0.05, .5]]
1976+
1977+
# set via color kwarg
1978+
ax_ref = fig_ref.subplots()
1979+
ax_ref.scatter(np.ones(3), range(3), color=rgb)
1980+
ax_ref.scatter(np.ones(4)*2, range(4), color=rgba)
1981+
1982+
# set via broadcasting via c
1983+
ax_test = fig_test.subplots()
1984+
ax_test.scatter(np.ones(3), range(3), c=rgb)
1985+
ax_test.scatter(np.ones(4)*2, range(4), c=rgba)
1986+
19711987

19721988
def _params(c=None, xsize=2, *, edgecolors=None, **kwargs):
19731989
return (c, edgecolors, kwargs if kwargs is not None else {}, xsize)

0 commit comments

Comments
 (0)
0