Skip to content

Commit 5977ca6

Browse files
committed
Shorten/make more consistent the half-filled marker definitions.
- Use dict lookups for fillstyle. - When possible, prefer defining `path` and `alt_path` to the same path and just make `alt_transform` the 180°-rotated version of `transform`. (This is consistent with what's already being done for "circle" and "octagon".) Doing so requires redefining the half-X and half-plus marker paths to not require a (-.5, -.5) translation but that actually makes the path symmetries more apparent.
1 parent 1eef019 commit 5977ca6

File tree

1 file changed

+46
-132
lines changed

1 file changed

+46
-132
lines changed

lib/matplotlib/markers.py

+46-132
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,9 @@ def _set_circle(self, reduction=1.0):
428428
if not self._half_fill():
429429
self._path = Path.unit_circle()
430430
else:
431-
# build a right-half circle
432-
if fs == 'bottom':
433-
rotate = 270.
434-
elif fs == 'top':
435-
rotate = 90.
436-
elif fs == 'left':
437-
rotate = 180.
438-
else:
439-
rotate = 0.
440-
441431
self._path = self._alt_path = Path.unit_circle_righthalf()
442-
self._transform.rotate_deg(rotate)
432+
self._transform.rotate_deg(
433+
{'right': 0, 'top': 90, 'left': 180, 'bottom': 270}[fs])
443434
self._alt_transform = self._transform.frozen().rotate_deg(180.)
444435

445436
def _set_pixel(self):
@@ -518,22 +509,12 @@ def _set_square(self):
518509
if not self._half_fill():
519510
self._path = Path.unit_rectangle()
520511
else:
521-
# build a bottom filled square out of two rectangles, one
522-
# filled. Use the rotation to support left, right, bottom
523-
# or top
524-
if fs == 'bottom':
525-
rotate = 0.
526-
elif fs == 'top':
527-
rotate = 180.
528-
elif fs == 'left':
529-
rotate = 270.
530-
else:
531-
rotate = 90.
532-
512+
# Build a bottom filled square out of two rectangles, one filled.
533513
self._path = Path([[0.0, 0.0], [1.0, 0.0], [1.0, 0.5],
534514
[0.0, 0.5], [0.0, 0.0]])
535515
self._alt_path = Path([[0.0, 0.5], [1.0, 0.5], [1.0, 1.0],
536516
[0.0, 1.0], [0.0, 0.5]])
517+
rotate = {'bottom': 0, 'right': 90, 'top': 180, 'left': 270}[fs]
537518
self._transform.rotate_deg(rotate)
538519
self._alt_transform = self._transform
539520

@@ -548,14 +529,7 @@ def _set_diamond(self):
548529
else:
549530
self._path = Path([[0, 0], [1, 0], [1, 1], [0, 0]])
550531
self._alt_path = Path([[0, 0], [0, 1], [1, 1], [0, 0]])
551-
if fs == 'bottom':
552-
rotate = 270.
553-
elif fs == 'top':
554-
rotate = 90.
555-
elif fs == 'left':
556-
rotate = 180.
557-
else:
558-
rotate = 0.
532+
rotate = {'right': 0, 'top': 90, 'left': 180, 'bottom': 270}[fs]
559533
self._transform.rotate_deg(rotate)
560534
self._alt_transform = self._transform
561535
self._joinstyle = 'miter'
@@ -575,23 +549,15 @@ def _set_pentagon(self):
575549
self._path = polypath
576550
else:
577551
verts = polypath.vertices
578-
579552
y = (1 + np.sqrt(5)) / 4.
580553
top = Path([verts[0], verts[1], verts[4], verts[0]])
581554
bottom = Path([verts[1], verts[2], verts[3], verts[4], verts[1]])
582555
left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
583556
right = Path([verts[0], verts[4], verts[3], [0, -y], verts[0]])
584-
585-
if fs == 'top':
586-
mpath, mpath_alt = top, bottom
587-
elif fs == 'bottom':
588-
mpath, mpath_alt = bottom, top
589-
elif fs == 'left':
590-
mpath, mpath_alt = left, right
591-
else:
592-
mpath, mpath_alt = right, left
593-
self._path = mpath
594-
self._alt_path = mpath_alt
557+
self._path, self._alt_path = {
558+
'top': (top, bottom), 'bottom': (bottom, top),
559+
'left': (left, right), 'right': (right, left),
560+
}[fs]
595561
self._alt_transform = self._transform
596562

597563
self._joinstyle = 'miter'
@@ -607,22 +573,14 @@ def _set_star(self):
607573
self._path = polypath
608574
else:
609575
verts = polypath.vertices
610-
611576
top = Path(np.concatenate([verts[0:4], verts[7:10], verts[0:1]]))
612577
bottom = Path(np.concatenate([verts[3:8], verts[3:4]]))
613578
left = Path(np.concatenate([verts[0:6], verts[0:1]]))
614579
right = Path(np.concatenate([verts[0:1], verts[5:10], verts[0:1]]))
615-
616-
if fs == 'top':
617-
mpath, mpath_alt = top, bottom
618-
elif fs == 'bottom':
619-
mpath, mpath_alt = bottom, top
620-
elif fs == 'left':
621-
mpath, mpath_alt = left, right
622-
else:
623-
mpath, mpath_alt = right, left
624-
self._path = mpath
625-
self._alt_path = mpath_alt
580+
self._path, self._alt_path = {
581+
'top': (top, bottom), 'bottom': (bottom, top),
582+
'left': (left, right), 'right': (right, left),
583+
}[fs]
626584
self._alt_transform = self._transform
627585

628586
self._joinstyle = 'bevel'
@@ -638,25 +596,16 @@ def _set_hexagon1(self):
638596
self._path = polypath
639597
else:
640598
verts = polypath.vertices
641-
642599
# not drawing inside lines
643600
x = np.abs(np.cos(5 * np.pi / 6.))
644601
top = Path(np.concatenate([[(-x, 0)], verts[[1, 0, 5]], [(x, 0)]]))
645602
bottom = Path(np.concatenate([[(-x, 0)], verts[2:5], [(x, 0)]]))
646603
left = Path(verts[0:4])
647604
right = Path(verts[[0, 5, 4, 3]])
648-
649-
if fs == 'top':
650-
mpath, mpath_alt = top, bottom
651-
elif fs == 'bottom':
652-
mpath, mpath_alt = bottom, top
653-
elif fs == 'left':
654-
mpath, mpath_alt = left, right
655-
else:
656-
mpath, mpath_alt = right, left
657-
658-
self._path = mpath
659-
self._alt_path = mpath_alt
605+
self._path, self._alt_path = {
606+
'top': (top, bottom), 'bottom': (bottom, top),
607+
'left': (left, right), 'right': (right, left),
608+
}[fs]
660609
self._alt_transform = self._transform
661610

662611
self._joinstyle = 'miter'
@@ -672,7 +621,6 @@ def _set_hexagon2(self):
672621
self._path = polypath
673622
else:
674623
verts = polypath.vertices
675-
676624
# not drawing inside lines
677625
x, y = np.sqrt(3) / 4, 3 / 4.
678626
top = Path(verts[[1, 0, 5, 4, 1]])
@@ -681,18 +629,10 @@ def _set_hexagon2(self):
681629
[(x, y)], verts[:3], [(-x, -y), (x, y)]]))
682630
right = Path(np.concatenate([
683631
[(x, y)], verts[5:2:-1], [(-x, -y)]]))
684-
685-
if fs == 'top':
686-
mpath, mpath_alt = top, bottom
687-
elif fs == 'bottom':
688-
mpath, mpath_alt = bottom, top
689-
elif fs == 'left':
690-
mpath, mpath_alt = left, right
691-
else:
692-
mpath, mpath_alt = right, left
693-
694-
self._path = mpath
695-
self._alt_path = mpath_alt
632+
self._path, self._alt_path = {
633+
'top': (top, bottom), 'bottom': (bottom, top),
634+
'left': (left, right), 'right': (right, left),
635+
}[fs]
696636
self._alt_transform = self._transform
697637

698638
self._joinstyle = 'miter'
@@ -711,17 +651,8 @@ def _set_octagon(self):
711651
x = np.sqrt(2.) / 4.
712652
half = Path([[0, -1], [0, 1], [-x, 1], [-1, x],
713653
[-1, -x], [-x, -1], [0, -1]])
714-
715-
if fs == 'bottom':
716-
rotate = 90.
717-
elif fs == 'top':
718-
rotate = 270.
719-
elif fs == 'right':
720-
rotate = 180.
721-
else:
722-
rotate = 0.
723-
724-
self._transform.rotate_deg(rotate)
654+
self._transform.rotate_deg(
655+
{'left': 0, 'bottom': 90, 'right': 180, 'top': 270}[fs])
725656
self._path = self._alt_path = half
726657
self._alt_transform = self._transform.frozen().rotate_deg(180.0)
727658

@@ -854,65 +785,48 @@ def _set_x(self):
854785
self._path = self._x_path
855786

856787
_plus_filled_path = Path(
857-
[(1/3, 0), (2/3, 0), (2/3, 1/3), (1, 1/3), (1, 2/3), (2/3, 2/3),
858-
(2/3, 1), (1/3, 1), (1/3, 2/3), (0, 2/3), (0, 1/3), (1/3, 1/3),
859-
(1/3, 0)], closed=True)
788+
np.array([(-1, -3), (+1, -3), (+1, -1), (+3, -1), (+3, +1), (+1, +1),
789+
(+1, +3), (-1, +3), (-1, +1), (-3, +1), (-3, -1), (-1, -1),
790+
(0, 0)]) / 6, closed=True)
860791
_plus_filled_path_t = Path(
861-
[(1, 1/2), (1, 2/3), (2/3, 2/3), (2/3, 1), (1/3, 1), (1/3, 2/3),
862-
(0, 2/3), (0, 1/2), (1, 1/2)], closed=True)
792+
np.array([(+3, 0), (+3, +1), (+1, +1), (+1, +3),
793+
(-1, +3), (-1, +1), (-3, +1), (-3, 0),
794+
(0, 0)]) / 6, closed=True)
863795

864796
def _set_plus_filled(self):
865-
self._transform = Affine2D().translate(-0.5, -0.5)
797+
self._transform = Affine2D()
866798
self._snap_threshold = 5.0
867799
self._joinstyle = 'miter'
868800
fs = self.get_fillstyle()
869801
if not self._half_fill():
870802
self._path = self._plus_filled_path
871803
else:
872804
# Rotate top half path to support all partitions
873-
if fs == 'top':
874-
rotate, rotate_alt = 0, 180
875-
elif fs == 'bottom':
876-
rotate, rotate_alt = 180, 0
877-
elif fs == 'left':
878-
rotate, rotate_alt = 90, 270
879-
else:
880-
rotate, rotate_alt = 270, 90
881-
882-
self._path = self._plus_filled_path_t
883-
self._alt_path = self._plus_filled_path_t
884-
self._alt_transform = Affine2D().translate(-0.5, -0.5)
885-
self._transform.rotate_deg(rotate)
886-
self._alt_transform.rotate_deg(rotate_alt)
805+
self._path = self._alt_path = self._plus_filled_path_t
806+
self._transform.rotate_deg({
807+
'top': 0, 'left': 90, 'bottom': 180, 'right': 270}[fs])
808+
self._alt_transform = self._transform.frozen().rotate_deg(180)
887809

888810
_x_filled_path = Path(
889-
[(0.25, 0), (0.5, 0.25), (0.75, 0), (1, 0.25), (0.75, 0.5), (1, 0.75),
890-
(0.75, 1), (0.5, 0.75), (0.25, 1), (0, 0.75), (0.25, 0.5), (0, 0.25),
891-
(0.25, 0)], closed=True)
811+
np.array([(-1, -2), (0, -1), (+1, -2), (+2, -1), (+1, 0), (+2, +1),
812+
(+1, +2), (0, +1), (-1, +2), (-2, +1), (-1, 0), (-2, -1),
813+
(0, 0)]) / 4,
814+
closed=True)
892815
_x_filled_path_t = Path(
893-
[(0.75, 0.5), (1, 0.75), (0.75, 1), (0.5, 0.75), (0.25, 1), (0, 0.75),
894-
(0.25, 0.5), (0.75, 0.5)], closed=True)
816+
np.array([(+1, 0), (+2, +1), (+1, +2), (0, +1),
817+
(-1, +2), (-2, +1), (-1, 0), (0, 0)]) / 4,
818+
closed=True)
895819

896820
def _set_x_filled(self):
897-
self._transform = Affine2D().translate(-0.5, -0.5)
821+
self._transform = Affine2D()
898822
self._snap_threshold = 5.0
899823
self._joinstyle = 'miter'
900824
fs = self.get_fillstyle()
901825
if not self._half_fill():
902826
self._path = self._x_filled_path
903827
else:
904828
# Rotate top half path to support all partitions
905-
if fs == 'top':
906-
rotate, rotate_alt = 0, 180
907-
elif fs == 'bottom':
908-
rotate, rotate_alt = 180, 0
909-
elif fs == 'left':
910-
rotate, rotate_alt = 90, 270
911-
else:
912-
rotate, rotate_alt = 270, 90
913-
914-
self._path = self._x_filled_path_t
915-
self._alt_path = self._x_filled_path_t
916-
self._alt_transform = Affine2D().translate(-0.5, -0.5)
917-
self._transform.rotate_deg(rotate)
918-
self._alt_transform.rotate_deg(rotate_alt)
829+
self._path = self._alt_path = self._x_filled_path_t
830+
self._transform.rotate_deg({
831+
'top': 0, 'left': 90, 'bottom': 180, 'right': 270}[fs])
832+
self._alt_transform = self._transform.frozen().rotate_deg(180)

0 commit comments

Comments
 (0)