Skip to content

Commit 16ae71f

Browse files
authored
Merge pull request #17543 from timhoffm/unfilled-markers
Fix linewidths and colors for scatter() with unfilled markers
2 parents 7a6c5d3 + d86cc2b commit 16ae71f

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

lib/matplotlib/axes/_axes.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4393,8 +4393,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
43934393
- 'none': No patch boundary will be drawn.
43944394
- A color or sequence of colors.
43954395
4396-
For non-filled markers, the *edgecolors* kwarg is ignored and
4397-
forced to 'face' internally.
4396+
For non-filled markers, *edgecolors* is ignored. Instead, the color
4397+
is determined like with 'face', i.e. from *c*, *colors*, or
4398+
*facecolors*.
43984399
43994400
plotnonfinite : bool, default: False
44004401
Set to plot points with nonfinite *c*, in conjunction with
@@ -4476,7 +4477,6 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44764477
path = marker_obj.get_path().transformed(
44774478
marker_obj.get_transform())
44784479
if not marker_obj.is_filled():
4479-
edgecolors = 'face'
44804480
if linewidths is None:
44814481
linewidths = rcParams['lines.linewidth']
44824482
elif np.iterable(linewidths):
@@ -4488,8 +4488,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
44884488

44894489
collection = mcoll.PathCollection(
44904490
(path,), scales,
4491-
facecolors=colors,
4492-
edgecolors=edgecolors,
4491+
facecolors=colors if marker_obj.is_filled() else 'none',
4492+
edgecolors=edgecolors if marker_obj.is_filled() else colors,
44934493
linewidths=linewidths,
44944494
offsets=offsets,
44954495
transOffset=kwargs.pop('transform', self.transData),

lib/matplotlib/markers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ def _recache(self):
232232
self._snap_threshold = None
233233
self._joinstyle = 'round'
234234
self._capstyle = 'butt'
235-
self._filled = True
235+
# Initial guess: Assume the marker is filled unless the fillstyle is
236+
# set to 'none'. The marker function will override this for unfilled
237+
# markers.
238+
self._filled = self._fillstyle != 'none'
236239
self._marker_function()
237240

238241
def __bool__(self):

lib/matplotlib/tests/test_axes.py

+10
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,16 @@ def test_scatter_color(self):
18371837
with pytest.raises(ValueError):
18381838
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
18391839

1840+
def test_scatter_unfilled(self):
1841+
coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'],
1842+
marker=mmarkers.MarkerStyle('o', fillstyle='none'),
1843+
linewidths=[1.1, 1.2, 1.3])
1844+
assert coll.get_facecolors().shape == (0, 4) # no facecolors
1845+
assert_array_equal(coll.get_edgecolors(), [[0.1, 0.1, 0.1, 1],
1846+
[0.3, 0.3, 0.3, 1],
1847+
[0.5, 0.5, 0.5, 1]])
1848+
assert_array_equal(coll.get_linewidths(), [1.1, 1.2, 1.3])
1849+
18401850
def test_scatter_size_arg_size(self):
18411851
x = np.arange(4)
18421852
with pytest.raises(ValueError):

lib/matplotlib/tests/test_marker.py

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import pytest
88

99

10+
def test_marker_fillstyle():
11+
marker_style = markers.MarkerStyle(marker='o', fillstyle='none')
12+
assert marker_style.get_fillstyle() == 'none'
13+
assert not marker_style.is_filled()
14+
15+
1016
def test_markers_valid():
1117
marker_style = markers.MarkerStyle()
1218
mrk_array = np.array([[-0.5, 0],

0 commit comments

Comments
 (0)