Skip to content

Allow MarkerStyle instances as input for lines #16692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/users/next_whats_new/2020-03-05_markerstyle-for-lines.rst
Original file line number Diff line number Diff line change
@@ -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"))

2 changes: 1 addition & 1 deletion lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
11 changes: 7 additions & 4 deletions lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)