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

Skip to content

Commit 220b1ef

Browse files
authored
Merge pull request #12739 from timhoffm/axes-parse-scatter-color-static
make Axes._parse_scatter_color_args static
2 parents 0ed35b1 + 54ebda9 commit 220b1ef

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
@@ -4018,7 +4018,9 @@ def dopatch(xs, ys, **kwargs):
40184018
return dict(whiskers=whiskers, caps=caps, boxes=boxes,
40194019
medians=medians, fliers=fliers, means=means)
40204020

4021-
def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
4021+
@staticmethod
4022+
def _parse_scatter_color_args(c, edgecolors, kwargs, xshape, yshape,
4023+
get_next_color_func):
40224024
"""
40234025
Helper function to process color related arguments of `.Axes.scatter`.
40244026
@@ -4028,7 +4030,7 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40284030
- kwargs['facecolors']
40294031
- kwargs['facecolor']
40304032
- kwargs['color'] (==kwcolor)
4031-
- 'b' if in classic mode else next color from color cycle
4033+
- 'b' if in classic mode else the result of ``get_next_color_func()``
40324034
40334035
Argument precedence for edgecolors:
40344036
@@ -4049,6 +4051,16 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40494051
Note: The dict is modified by this function.
40504052
xshape, yshape : tuple of int
40514053
The shape of the x and y arrays passed to `.Axes.scatter`.
4054+
get_next_color_func : callable
4055+
A callable that returns a color. This color is used as facecolor
4056+
if no other color is provided.
4057+
4058+
Note, that this is a function rather than a fixed color value to
4059+
support conditional evaluation of the next color. As of the
4060+
current implementation obtaining the next color from the
4061+
property cycle advances the cycle. This must only happen if we
4062+
actually use the color, which will only be decided within this
4063+
method.
40524064
40534065
Returns
40544066
-------
@@ -4095,7 +4107,7 @@ def _parse_scatter_color_args(self, c, edgecolors, kwargs, xshape, yshape):
40954107
if c is None:
40964108
c = (facecolors if facecolors is not None
40974109
else "b" if rcParams['_internal.classic_mode']
4098-
else self._get_patches_for_fill.get_next_color())
4110+
else get_next_color_func())
40994111

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

42964308
c, colors, edgecolors = \
4297-
self._parse_scatter_color_args(c, edgecolors, kwargs,
4298-
xshape, yshape)
4309+
self._parse_scatter_color_args(
4310+
c, edgecolors, kwargs, xshape, yshape,
4311+
get_next_color_func=self._get_patches_for_fill.get_next_color)
42994312

43004313
# `delete_masked_points` only modifies arguments of the same length as
43014314
# `x`.

lib/matplotlib/tests/test_axes.py

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

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

18051810
if re_key is None:
1806-
ax.scatter(x, y, c=c_case, edgecolors="black")
1811+
Axes._parse_scatter_color_args(
1812+
c=c_case, edgecolors="black", kwargs={},
1813+
xshape=xshape, yshape=yshape,
1814+
get_next_color_func=get_next_color)
18071815
else:
18081816
with pytest.raises(ValueError, match=REGEXP[re_key]):
1809-
ax.scatter(x, y, c=c_case, edgecolors="black")
1817+
Axes._parse_scatter_color_args(
1818+
c=c_case, edgecolors="black", kwargs={},
1819+
xshape=xshape, yshape=yshape,
1820+
get_next_color_func=get_next_color)
18101821

18111822

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

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

18701883

0 commit comments

Comments
 (0)