Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 460805d

Browse files
authored
Merge pull request #17501 from tacaswell/auto-backport-of-pr-17499-on-v3.2.x
Backport PR #17499: Fix scatter singlecolor
2 parents b47e31d + 871bbfc commit 460805d

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
@@ -4212,19 +4212,26 @@ def invalid_shape_exception(csize, xsize):
42124212
except ValueError:
42134213
pass # Failed to convert to float array; must be color specs.
42144214
else:
4215+
# handle the documented special case of a 2D array with 1
4216+
# row which as RGB(A) to broadcast.
4217+
if c.shape == (1, 4) or c.shape == (1, 3):
4218+
c_is_mapped = False
4219+
if c.size != xsize:
4220+
valid_shape = False
42154221
# If c can be either mapped values or a RGB(A) color, prefer
42164222
# the former if shapes match, the latter otherwise.
4217-
if c.size == xsize:
4223+
elif c.size == xsize:
42184224
c = c.ravel()
42194225
c_is_mapped = True
42204226
else: # Wrong size; it must not be intended for mapping.
42214227
if c.shape in ((3,), (4,)):
42224228
_log.warning(
4223-
"'c' argument looks like a single numeric RGB or "
4229+
"*c* argument looks like a single numeric RGB or "
42244230
"RGBA sequence, which should be avoided as value-"
42254231
"mapping will have precedence in case its length "
4226-
"matches with 'x' & 'y'. Please use a 2-D array "
4227-
"with a single row if you really want to specify "
4232+
"matches with *x* & *y*. Please use the *color* "
4233+
"keyword-argument or provide a 2-D array "
4234+
"with a single row if you intend to specify "
42284235
"the same RGB or RGBA value for all points.")
42294236
valid_shape = False
42304237
if not c_is_mapped:
@@ -4268,14 +4275,14 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42684275
The marker size in points**2.
42694276
Default is ``rcParams['lines.markersize'] ** 2``.
42704277
4271-
c : color, sequence, or sequence of colors, optional
4272-
The marker color. Possible values:
4278+
c : array-like or list of colors or color, optional
4279+
The marker colors. Possible values:
42734280
4274-
- A single color format string.
4275-
- A sequence of colors of length n.
42764281
- A scalar or sequence of n numbers to be mapped to colors using
42774282
*cmap* and *norm*.
42784283
- A 2-D array in which the rows are RGB or RGBA.
4284+
- A sequence of colors of length n.
4285+
- A single color format string.
42794286
42804287
Note that *c* should not be a single numeric RGB or RGBA sequence
42814288
because that is indistinguishable from an array of values to be
@@ -4284,9 +4291,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42844291
matching will have precedence in case of a size matching with *x*
42854292
and *y*.
42864293
4287-
Defaults to ``None``. In that case the marker color is determined
4288-
by the value of ``color``, ``facecolor`` or ``facecolors``. In case
4289-
those are not specified or ``None``, the marker color is determined
4294+
If you wish to specify a single color for all points
4295+
prefer the *color* keyword argument.
4296+
4297+
Defaults to `None`. In that case the marker color is determined
4298+
by the value of *color*, *facecolor* or *facecolors*. In case
4299+
those are not specified or `None`, the marker color is determined
42904300
by the next color of the ``Axes``' current "shape and fill" color
42914301
cycle. This cycle defaults to :rc:`axes.prop_cycle`.
42924302

lib/matplotlib/tests/test_axes.py

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

2091+
@pytest.mark.style('default')
2092+
@check_figures_equal(extensions=["png"])
2093+
def test_scatter_single_color_c(self, fig_test, fig_ref):
2094+
rgb = [[1, 0.5, 0.05]]
2095+
rgba = [[1, 0.5, 0.05, .5]]
2096+
2097+
# set via color kwarg
2098+
ax_ref = fig_ref.subplots()
2099+
ax_ref.scatter(np.ones(3), range(3), color=rgb)
2100+
ax_ref.scatter(np.ones(4)*2, range(4), color=rgba)
2101+
2102+
# set via broadcasting via c
2103+
ax_test = fig_test.subplots()
2104+
ax_test.scatter(np.ones(3), range(3), c=rgb)
2105+
ax_test.scatter(np.ones(4)*2, range(4), c=rgba)
2106+
20912107

20922108
def _params(c=None, xsize=2, **kwargs):
20932109
edgecolors = kwargs.pop('edgecolors', None)

0 commit comments

Comments
 (0)