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

Skip to content

Commit d84f4a7

Browse files
committed
FIX: fix support for single color 2D array as c to mean single color
Closes #17423
1 parent fb9e723 commit d84f4a7

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4281,9 +4281,15 @@ 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.

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)