diff --git a/doc/users/next_whats_new/2020-03-05_markerstyle-for-lines.rst b/doc/users/next_whats_new/2020-03-05_markerstyle-for-lines.rst new file mode 100644 index 000000000000..81dbec49c7fc --- /dev/null +++ b/doc/users/next_whats_new/2020-03-05_markerstyle-for-lines.rst @@ -0,0 +1,7 @@ +Lines now accept ``MarkerStyle`` instances as input +--------------------------------------------------- +Similar to `~.Axes.scatter`, `~.Axes.plot` and `~.lines.Line2D` now accept +`~.markers.MarkerStyle` instances as input for the *marker* parameter:: + + plt.plot(..., marker=matplotlib.markers.MarkerStyle("D")) + diff --git a/lib/matplotlib/lines.py b/lib/matplotlib/lines.py index 76c256dfa185..775399278cdf 100644 --- a/lib/matplotlib/lines.py +++ b/lib/matplotlib/lines.py @@ -1195,7 +1195,7 @@ def set_marker(self, marker): Parameters ---------- - marker : marker style + marker : marker style string, `~.path.Path` or `~.markers.MarkerStyle` See `~matplotlib.markers` for full description of possible arguments. """ diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index bab0d4a600b8..0285b4b3dd06 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -283,16 +283,19 @@ def set_marker(self, marker): marker in self.markers): self._marker_function = getattr( self, '_set_' + self.markers[marker]) + elif isinstance(marker, MarkerStyle): + self.__dict__.update(marker.__dict__) else: try: Path(marker) self._marker_function = self._set_vertices - except ValueError: + except ValueError as err: raise ValueError('Unrecognized marker style {!r}' - .format(marker)) + .format(marker)) from err - self._marker = marker - self._recache() + if not isinstance(marker, MarkerStyle): + self._marker = marker + self._recache() def get_path(self): return self._path diff --git a/lib/matplotlib/tests/test_lines.py b/lib/matplotlib/tests/test_lines.py index 517b467e0994..d66509b0ee78 100644 --- a/lib/matplotlib/tests/test_lines.py +++ b/lib/matplotlib/tests/test_lines.py @@ -7,10 +7,13 @@ from cycler import cycler import numpy as np +from numpy.testing import assert_array_equal import pytest import matplotlib import matplotlib.lines as mlines +from matplotlib.markers import MarkerStyle +from matplotlib.path import Path import matplotlib.pyplot as plt from matplotlib.testing.decorators import image_comparison, check_figures_equal @@ -194,3 +197,23 @@ def test_nan_is_sorted(): def test_step_markers(fig_test, fig_ref): fig_test.subplots().step([0, 1], "-o") fig_ref.subplots().plot([0, 0, 1], [0, 1, 1], "-o", markevery=[0, 2]) + + +def test_marker_as_markerstyle(): + fig, ax = plt.subplots() + line, = ax.plot([2, 4, 3], marker=MarkerStyle("D")) + fig.canvas.draw() + assert line.get_marker() == "D" + + # continue with smoke tests: + line.set_marker("s") + fig.canvas.draw() + line.set_marker(MarkerStyle("o")) + fig.canvas.draw() + # test Path roundtrip + triangle1 = Path([[-1., -1.], [1., -1.], [0., 2.], [0., 0.]], closed=True) + line2, = ax.plot([1, 3, 2], marker=MarkerStyle(triangle1), ms=22) + line3, = ax.plot([0, 2, 1], marker=triangle1, ms=22) + + assert_array_equal(line2.get_marker().vertices, triangle1.vertices) + assert_array_equal(line3.get_marker().vertices, triangle1.vertices)