Skip to content

Commit 603cf1a

Browse files
authored
Merge pull request #17850 from tacaswell/mnt_nofill_facecolor
MNT: set the facecolor of nofill markers
2 parents 3515d25 + d206a0e commit 603cf1a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/matplotlib/axes/_axes.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -4490,6 +4490,10 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44904490
"s must be a scalar, "
44914491
"or float array-like with the same size as x and y")
44924492

4493+
# get the original edgecolor the user passed before we normalize
4494+
orig_edgecolor = edgecolors
4495+
if edgecolors is None:
4496+
orig_edgecolor = kwargs.get('edgecolor', None)
44934497
c, colors, edgecolors = \
44944498
self._parse_scatter_color_args(
44954499
c, edgecolors, kwargs, x.size,
@@ -4518,6 +4522,36 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
45184522
path = marker_obj.get_path().transformed(
45194523
marker_obj.get_transform())
45204524
if not marker_obj.is_filled():
4525+
if orig_edgecolor is not None:
4526+
_api.warn_external(
4527+
f"You passed a edgecolor/edgecolors ({orig_edgecolor!r}) "
4528+
f"for an unfilled marker ({marker!r}). Matplotlib is "
4529+
"ignoring the edgecolor in favor of the facecolor. This "
4530+
"behavior may change in the future."
4531+
)
4532+
# We need to handle markers that can not be filled (like
4533+
# '+' and 'x') differently than markers that can be
4534+
# filled, but have their fillstyle set to 'none'. This is
4535+
# to get:
4536+
#
4537+
# - respecting the fillestyle if set
4538+
# - maintaining back-compatibility for querying the facecolor of
4539+
# the un-fillable markers.
4540+
#
4541+
# While not an ideal situation, but is better than the
4542+
# alternatives.
4543+
if marker_obj.get_fillstyle() == 'none':
4544+
# promote the facecolor to be the edgecolor
4545+
edgecolors = colors
4546+
# set the facecolor to 'none' (at the last chance) because
4547+
# we can not not fill a path if the facecolor is non-null.
4548+
# (which is defendable at the renderer level)
4549+
colors = 'none'
4550+
else:
4551+
# if we are not nulling the face color we can do this
4552+
# simpler
4553+
edgecolors = 'face'
4554+
45214555
if linewidths is None:
45224556
linewidths = rcParams['lines.linewidth']
45234557
elif np.iterable(linewidths):
@@ -4529,8 +4563,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
45294563

45304564
collection = mcoll.PathCollection(
45314565
(path,), scales,
4532-
facecolors=colors if marker_obj.is_filled() else 'none',
4533-
edgecolors=edgecolors if marker_obj.is_filled() else colors,
4566+
facecolors=colors,
4567+
edgecolors=edgecolors,
45344568
linewidths=linewidths,
45354569
offsets=offsets,
45364570
transOffset=kwargs.pop('transform', self.transData),

lib/matplotlib/tests/test_axes.py

+21
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,17 @@ def test_scatter_unfilled(self):
21452145
[0.5, 0.5, 0.5, 1]])
21462146
assert_array_equal(coll.get_linewidths(), [1.1, 1.2, 1.3])
21472147

2148+
@pytest.mark.style('default')
2149+
def test_scatter_unfillable(self):
2150+
coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'],
2151+
marker='x',
2152+
linewidths=[1.1, 1.2, 1.3])
2153+
assert_array_equal(coll.get_facecolors(), coll.get_edgecolors())
2154+
assert_array_equal(coll.get_edgecolors(), [[0.1, 0.1, 0.1, 1],
2155+
[0.3, 0.3, 0.3, 1],
2156+
[0.5, 0.5, 0.5, 1]])
2157+
assert_array_equal(coll.get_linewidths(), [1.1, 1.2, 1.3])
2158+
21482159
def test_scatter_size_arg_size(self):
21492160
x = np.arange(4)
21502161
with pytest.raises(ValueError, match='same size as x and y'):
@@ -6939,3 +6950,13 @@ def test_patch_bounds(): # PR 19078
69396950
bot = 1.9*np.sin(15*np.pi/180)**2
69406951
np.testing.assert_array_almost_equal_nulp(
69416952
np.array((-0.525, -(bot+0.05), 1.05, bot+0.1)), ax.dataLim.bounds, 16)
6953+
6954+
6955+
@pytest.mark.style('default')
6956+
def test_warn_ignored_scatter_kwargs():
6957+
with pytest.warns(UserWarning,
6958+
match=r"You passed a edgecolor/edgecolors"):
6959+
6960+
c = plt.scatter(
6961+
[0], [0], marker="+", s=500, facecolor="r", edgecolor="b"
6962+
)

0 commit comments

Comments
 (0)