Skip to content

Commit 071da48

Browse files
authored
Merge pull request #16692 from ImportanceOfBeingErnest/marker-for-lines
Allow MarkerStyle instances as input for lines
2 parents e41277c + ac8c2bf commit 071da48

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Lines now accept ``MarkerStyle`` instances as input
2+
---------------------------------------------------
3+
Similar to `~.Axes.scatter`, `~.Axes.plot` and `~.lines.Line2D` now accept
4+
`~.markers.MarkerStyle` instances as input for the *marker* parameter::
5+
6+
plt.plot(..., marker=matplotlib.markers.MarkerStyle("D"))
7+

lib/matplotlib/lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ def set_marker(self, marker):
11971197
11981198
Parameters
11991199
----------
1200-
marker : marker style
1200+
marker : marker style string, `~.path.Path` or `~.markers.MarkerStyle`
12011201
See `~matplotlib.markers` for full description of possible
12021202
arguments.
12031203
"""

lib/matplotlib/markers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ def set_marker(self, marker):
283283
marker in self.markers):
284284
self._marker_function = getattr(
285285
self, '_set_' + self.markers[marker])
286+
elif isinstance(marker, MarkerStyle):
287+
self.__dict__.update(marker.__dict__)
286288
else:
287289
try:
288290
Path(marker)
@@ -291,8 +293,9 @@ def set_marker(self, marker):
291293
raise ValueError('Unrecognized marker style {!r}'
292294
.format(marker)) from err
293295

294-
self._marker = marker
295-
self._recache()
296+
if not isinstance(marker, MarkerStyle):
297+
self._marker = marker
298+
self._recache()
296299

297300
def get_path(self):
298301
return self._path

lib/matplotlib/tests/test_lines.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
from cycler import cycler
99
import numpy as np
10+
from numpy.testing import assert_array_equal
1011
import pytest
1112

1213
import matplotlib
1314
import matplotlib.lines as mlines
15+
from matplotlib.markers import MarkerStyle
16+
from matplotlib.path import Path
1417
import matplotlib.pyplot as plt
1518
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1619

@@ -194,3 +197,23 @@ def test_nan_is_sorted():
194197
def test_step_markers(fig_test, fig_ref):
195198
fig_test.subplots().step([0, 1], "-o")
196199
fig_ref.subplots().plot([0, 0, 1], [0, 1, 1], "-o", markevery=[0, 2])
200+
201+
202+
def test_marker_as_markerstyle():
203+
fig, ax = plt.subplots()
204+
line, = ax.plot([2, 4, 3], marker=MarkerStyle("D"))
205+
fig.canvas.draw()
206+
assert line.get_marker() == "D"
207+
208+
# continue with smoke tests:
209+
line.set_marker("s")
210+
fig.canvas.draw()
211+
line.set_marker(MarkerStyle("o"))
212+
fig.canvas.draw()
213+
# test Path roundtrip
214+
triangle1 = Path([[-1., -1.], [1., -1.], [0., 2.], [0., 0.]], closed=True)
215+
line2, = ax.plot([1, 3, 2], marker=MarkerStyle(triangle1), ms=22)
216+
line3, = ax.plot([0, 2, 1], marker=triangle1, ms=22)
217+
218+
assert_array_equal(line2.get_marker().vertices, triangle1.vertices)
219+
assert_array_equal(line3.get_marker().vertices, triangle1.vertices)

0 commit comments

Comments
 (0)