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

Skip to content

Commit 54ebda9

Browse files
committed
make Axes._parse_scatter_color_args static
1 parent ac0525e commit 54ebda9

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4013,7 +4013,9 @@ def dopatch(xs, ys, **kwargs):
40134013
return dict(whiskers=whiskers, caps=caps, boxes=boxes,
40144014
medians=medians, fliers=fliers, means=means)
40154015

4016-
def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
4016+
@staticmethod
4017+
def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4018+
get_next_color_func):
40174019
"""
40184020
Helper function to process color related arguments of `.Axes.scatter`.
40194021
@@ -4023,7 +4025,7 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40234025
- kwargs['facecolors']
40244026
- kwargs['facecolor']
40254027
- kwargs['color'] (==kwcolor)
4026-
- 'b' if in classic mode else next color from color cycle
4028+
- 'b' if in classic mode else the result of ``get_next_color_func()``
40274029
40284030
Argument precedence for edgecolors:
40294031
@@ -4044,6 +4046,16 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40444046
Note: The dict is modified by this function.
40454047
xshape, yshape : tuple of int
40464048
The shape of the x and y arrays passed to `.Axes.scatter`.
4049+
get_next_color_func : callable
4050+
A callable that returns a color. This color is used as facecolor
4051+
if no other color is provided.
4052+
4053+
Note, that this is a function rather than a fixed color value to
4054+
support conditional evaluation of the next color. As of the
4055+
current implementation obtaining the next color from the
4056+
property cycle advances the cycle. This must only happen if we
4057+
actually use the color, which will only be decided within this
4058+
method.
40474059
40484060
Returns
40494061
-------
@@ -4090,7 +4102,7 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40904102
if c is None:
40914103
c = (facecolors if facecolors is not None
40924104
else "b" if rcParams['_internal.classic_mode']
4093-
else self._get_patches_for_fill.get_next_color())
4105+
else get_next_color_func())
40944106

40954107
# After this block, c_array will be None unless
40964108
# c is an array for mapping. The potential ambiguity
@@ -4289,8 +4301,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42894301
s = np.ma.ravel(s) # This doesn't have to match x, y in size.
42904302

42914303
c, colors, edgecolors = \
4292-
self._parse_scatter_color_args(c, edgecolors, kwargs,
4293-
xshape, yshape)
4304+
self._parse_scatter_color_args(
4305+
c, edgecolors, kwargs, xshape, yshape,
4306+
get_next_color_func=self._get_patches_for_fill.get_next_color)
42944307

42954308
# `delete_masked_points` only modifies arguments of the same length as
42964309
# `x`.

lib/matplotlib/tests/test_axes.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,19 +1793,30 @@ def test_scatter_color(self):
17931793

17941794
@pytest.mark.parametrize('c_case, re_key', params_test_scatter_c)
17951795
def test_scatter_c(self, c_case, re_key):
1796+
def get_next_color():
1797+
return 'blue' # currently unused
1798+
1799+
from matplotlib.axes import Axes
1800+
1801+
xshape = yshape = (4,)
1802+
17961803
# Additional checking of *c* (introduced in #11383).
17971804
REGEXP = {
17981805
"shape": "^'c' argument has [0-9]+ elements", # shape mismatch
17991806
"conversion": "^'c' argument must be a mpl color", # bad vals
18001807
}
1801-
x = y = [0, 1, 2, 3]
1802-
fig, ax = plt.subplots()
18031808

18041809
if re_key is None:
1805-
ax.scatter(x, y, c=c_case, edgecolors="black")
1810+
Axes._parse_scatter_color_args(
1811+
c=c_case, edgecolors="black", kwargs={},
1812+
xshape=xshape, yshape=yshape,
1813+
get_next_color_func=get_next_color)
18061814
else:
18071815
with pytest.raises(ValueError, match=REGEXP[re_key]):
1808-
ax.scatter(x, y, c=c_case, edgecolors="black")
1816+
Axes._parse_scatter_color_args(
1817+
c=c_case, edgecolors="black", kwargs={},
1818+
xshape=xshape, yshape=yshape,
1819+
get_next_color_func=get_next_color)
18091820

18101821

18111822
def _params(c=None, xshape=(2,), yshape=(2,), **kwargs):
@@ -1829,11 +1840,12 @@ def _params(c=None, xshape=(2,), yshape=(2,), **kwargs):
18291840
_result(c=['b', 'g'], colors=np.array([[0, 0, 1, 1], [0, .5, 0, 1]]))),
18301841
])
18311842
def test_parse_scatter_color_args(params, expected_result):
1843+
def get_next_color():
1844+
return 'blue' # currently unused
1845+
18321846
from matplotlib.axes import Axes
1833-
dummyself = 'UNUSED' # self is only used in one case, which we do not
1834-
# test. Therefore we can get away without costly
1835-
# creating an Axes instance.
1836-
c, colors, _edgecolors = Axes._parse_scatter_color_args(dummyself, *params)
1847+
c, colors, _edgecolors = Axes._parse_scatter_color_args(
1848+
*params, get_next_color_func=get_next_color)
18371849
assert c == expected_result.c
18381850
assert_allclose(colors, expected_result.colors)
18391851

@@ -1855,15 +1867,16 @@ def test_parse_scatter_color_args(params, expected_result):
18551867
(dict(color='r', edgecolor='g'), 'g'),
18561868
])
18571869
def test_parse_scatter_color_args_edgecolors(kwargs, expected_edgecolors):
1870+
def get_next_color():
1871+
return 'blue' # currently unused
1872+
18581873
from matplotlib.axes import Axes
1859-
dummyself = 'UNUSED' # self is only used in one case, which we do not
1860-
# test. Therefore we can get away without costly
1861-
# creating an Axes instance.
18621874
c = kwargs.pop('c', None)
18631875
edgecolors = kwargs.pop('edgecolors', None)
18641876
_, _, result_edgecolors = \
1865-
Axes._parse_scatter_color_args(dummyself, c, edgecolors, kwargs,
1866-
xshape=(2,), yshape=(2,))
1877+
Axes._parse_scatter_color_args(c, edgecolors, kwargs,
1878+
xshape=(2,), yshape=(2,),
1879+
get_next_color_func=get_next_color)
18671880
assert result_edgecolors == expected_edgecolors
18681881

18691882

0 commit comments

Comments
 (0)