8000 Updated marker documentation & added example of using a Path with a m… · matplotlib/matplotlib@3914e5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3914e5d

Browse files
author
Phil Elson
committed
Updated marker documentation & added example of using a Path with a marker.
1 parent 9a8b35e commit 3914e5d

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.path as mpath
3+
import numpy as np
4+
5+
6+
star = mpath.Path.unit_regular_star(6)
7+
circle = mpath.Path.unit_circle()
8+
# concatenate the star with an internal cutout of the circle
9+
verts = np.concatenate([star.vertices, circle.vertices[::-1, ...]])
10+
codes = np.concatenate([star.codes, circle.codes])
11+
cut_star = mpath.Path(verts, codes)
12+
13+
14+
plt.plot(np.arange(10)**2, '--r', marker=cut_star, markersize=15)
15+
16+
plt.show()

lib/matplotlib/markers.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class MarkerStyle:
2424
marker description
2525
============================== ===============================================
2626
%s
27-
``'$...$'`` render the string using mathtext
28-
*verts* a list of (x, y) pairs in range (0, 1)
27+
``'$...$'`` render the string using mathtext.
28+
*verts* a list of (x, y) pairs used for Path vertices.
29+
path a :class:`~matplotlib.path.Path` instance.
2930
(*numsides*, *style*, *angle*) see below
3031
============================== ===============================================
3132
@@ -150,6 +151,8 @@ def set_marker(self, marker):
150151
if (iterable(marker) and len(marker) in (2, 3) and
151152
marker[1] in (0, 1, 2, 3)):
152153
self._marker_function = self._set_tuple_marker
154+
elif isinstance(marker, np.ndarray):
155+
self._marker_function = self._set_vertices
153156
elif marker in self.markers:
154157
self._marker_function = getattr(
155158
self, '_set_' + self.markers[marker])
@@ -159,10 +162,10 @@ def set_marker(self, marker):
159162
self._marker_function = self._set_path_marker
160163
else:
161164
try:
162-
path = Path(marker)
165+
_ = Path(marker)
163166
self._marker_function = self._set_vertices
164-
except:
165-
raise ValueError('Unrecognized marker style %s' % marker)
167+
except ValueError:
168+
raise ValueError('Unrecognized marker style {}'.format(marker))
166169

167170
self._marker = marker
168171
self._recache()
@@ -195,6 +198,8 @@ def _set_path_marker(self):
195198
self._set_custom_marker(self._marker)
196199

197200
def _set_vertices(self):
201+
# XXX how can this possibly work?
202+
verts = self._marker
198203
path = Path(verts)
199204
self._set_custom_marker(path)
200205

@@ -230,7 +235,6 @@ def _set_mathtext_path(self):
230235
231236
Submitted by tcb
232237
"""
233-
from matplotlib.patches import PathPatch
234238
from matplotlib.text import TextPath
235239
from matplotlib.font_manager import FontProperties
236240

@@ -437,10 +441,10 @@ def _set_star(self):
437441
else:
438442
verts = polypath.vertices
439443

440-
top = Path(np.vstack((verts[0:4,:], verts[7:10,:], verts[0])))
441-
bottom = Path(np.vstack((verts[3:8,:], verts[3])))
442-
left = Path(np.vstack((verts[0:6,:], verts[0])))
443-
right = Path(np.vstack((verts[0], verts[5:10,:], verts[0])))
444+
top = Path(np.vstack((verts[0:4, :], verts[7:10, :], verts[0])))
445+
bottom = Path(np.vstack((verts[3:8, :], verts[3])))
446+
left = Path(np.vstack((verts[0:6, :], verts[0])))
447+
right = Path(np.vstack((verts[0], verts[5:10, :], verts[0])))
444448

445449
if fs == 'top':
446450
mpath, mpath_alt = top, bottom
@@ -470,10 +474,10 @@ def _set_hexagon1(self):
470474

471475
# not drawing inside lines
472476
x = np.abs(np.cos(5*np.pi/6.))
473-
top = Path(np.vstack(([-x,0],verts[(1,0,5),:],[x,0])))
474-
bottom = Path(np.vstack(([-x,0],verts[2:5,:],[x,0])))
475-
left = Path(verts[(0,1,2,3),:])
476-
right = Path(verts[(0,5,4,3),:])
477+
top = Path(np.vstack(([-x, 0], verts[(1, 0, 5), :], [x, 0])))
478+
bottom = Path(np.vstack(([-x, 0], verts[2:5, :], [x, 0])))
479+
left = Path(verts[(0, 1, 2, 3), :])
480+
right = Path(verts[(0, 5, 4, 3), :])
477481

478482
if fs == 'top':
479483
mpath, mpath_alt = top, bottom
@@ -504,10 +508,10 @@ def _set_hexagon2(self):
504508

505509
# not drawing inside lines
506510
x, y = np.sqrt(3)/4, 3/4.
507-
top = Path(verts[(1,0,5,4,1),:])
508-
bottom = Path(verts[(1,2,3,4),:])
509-
left = Path(np.vstack(([x,y],verts[(0,1,2),:],[-x,-y],[x,y])))
510-
right = Path(np.vstack(([x,y],verts[(5,4,3),:],[-x,-y])))
511+
top = Path(verts[(1, 0, 5, 4, 1), :])
512+
bottom = Path(verts[(1, 2, 3, 4), :])
513+
left = Path(np.vstack(([x, y], verts[(0, 1, 2), :], [-x, -y], [x, y])))
514+
right = Path(np.vstack(([x, y], verts[(5, 4, 3), :], [-x, -y])))
511515

512516
if fs == 'top':
513517
mpath, mpath_alt = top, bottom

0 commit comments

Comments
 (0)
0