Skip to content

Commit b52d646

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".)
1 parent 1eef019 commit b52d646

File tree

1 file changed

+38
-130
lines changed

1 file changed

+38
-130
lines changed

lib/matplotlib/markers.py

Lines changed: 38 additions & 130 deletions
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,24 +509,11 @@ 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-
533-
self._path = Path([[0.0, 0.0], [1.0, 0.0], [1.0, 0.5],
534-
[0.0, 0.5], [0.0, 0.0]])
535-
self._alt_path = Path([[0.0, 0.5], [1.0, 0.5], [1.0, 1.0],
536-
[0.0, 1.0], [0.0, 0.5]])
537-
self._transform.rotate_deg(rotate)
538-
self._alt_transform = self._transform
512+
self._path = self._alt_path = Path( # Half-square.
513+
[[0.0, 0.0], [1.0, 0.0], [1.0, 0.5], [0.0, 0.5], [0.0, 0.0]])
514+
self._transform.rotate_deg(
515+
{'bottom': 0, 'right': 90, 'top': 180, 'left': 270}[fs])
516+
self._alt_transform = self._transform.frozen().rotate_deg(180)
539517

540518
self._joinstyle = 'miter'
541519

@@ -546,18 +524,11 @@ def _set_diamond(self):
546524
if not self._half_fill():
547525
self._path = Path.unit_rectangle()
548526
else:
549-
self._path = Path([[0, 0], [1, 0], [1, 1], [0, 0]])
550-
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.
559-
self._transform.rotate_deg(rotate)
560-
self._alt_transform = self._transform
527+
self._path = self._alt_path = Path(
528+
[[0, 0], [1, 0], [1, 1], [0, 0]])
529+
self._transform.rotate_deg(
530+
{'right': 0, 'top': 90, 'left': 180, 'bottom': 270}[fs])
531+
self._alt_transform = self._transform.frozen().rotate_deg(180)
561532
self._joinstyle = 'miter'
562533

563534
def _set_thin_diamond(self):
@@ -575,23 +546,15 @@ def _set_pentagon(self):
575546
self._path = polypath
576547
else:
577548
verts = polypath.vertices
578-
579549
y = (1 + np.sqrt(5)) / 4.
580550
top = Path([verts[0], verts[1], verts[4], verts[0]])
581551
bottom = Path([verts[1], verts[2], verts[3], verts[4], verts[1]])
582552
left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
583553
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
554+
self._path, self._alt_path = {
555+
'top': (top, bottom), 'bottom': (bottom, top),
556+
'left': (left, right), 'right': (right, left),
557+
}[fs]
595558
self._alt_transform = self._transform
596559

597560
self._joinstyle = 'miter'
@@ -607,22 +570,14 @@ def _set_star(self):
607570
self._path = polypath
608571
else:
609572
verts = polypath.vertices
610-
611573
top = Path(np.concatenate([verts[0:4], verts[7:10], verts[0:1]]))
612574
bottom = Path(np.concatenate([verts[3:8], verts[3:4]]))
613575
left = Path(np.concatenate([verts[0:6], verts[0:1]]))
614576
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
577+
self._path, self._alt_path = {
578+
'top': (top, bottom), 'bottom': (bottom, top),
579+
'left': (left, right), 'right': (right, left),
580+
}[fs]
626581
self._alt_transform = self._transform
627582

628583
self._joinstyle = 'bevel'
@@ -638,25 +593,16 @@ def _set_hexagon1(self):
638593
self._path = polypath
639594
else:
640595
verts = polypath.vertices
641-
642596
# not drawing inside lines
643597
x = np.abs(np.cos(5 * np.pi / 6.))
644598
top = Path(np.concatenate([[(-x, 0)], verts[[1, 0, 5]], [(x, 0)]]))
645599
bottom = Path(np.concatenate([[(-x, 0)], verts[2:5], [(x, 0)]]))
646600
left = Path(verts[0:4])
647601
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
602+
self._path, self._alt_path = {
603+
'top': (top, bottom), 'bottom': (bottom, top),
604+
'left': (left, right), 'right': (right, left),
605+
}[fs]
660606
self._alt_transform = self._transform
661607

662608
self._joinstyle = 'miter'
@@ -672,7 +618,6 @@ def _set_hexagon2(self):
672618
self._path = polypath
673619
else:
674620
verts = polypath.vertices
675-
676621
# not drawing inside lines
677622
x, y = np.sqrt(3) / 4, 3 / 4.
678623
top = Path(verts[[1, 0, 5, 4, 1]])
@@ -681,18 +626,10 @@ def _set_hexagon2(self):
681626
[(x, y)], verts[:3], [(-x, -y), (x, y)]]))
682627
right = Path(np.concatenate([
683628
[(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
629+
self._path, self._alt_path = {
630+
'top': (top, bottom), 'bottom': (bottom, top),
631+
'left': (left, right), 'right': (right, left),
632+
}[fs]
696633
self._alt_transform = self._transform
697634

698635
self._joinstyle = 'miter'
@@ -711,17 +648,8 @@ def _set_octagon(self):
711648
x = np.sqrt(2.) / 4.
712649
half = Path([[0, -1], [0, 1], [-x, 1], [-1, x],
713650
[-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)
651+
self._transform.rotate_deg(
652+
{'left': 0, 'bottom': 90, 'right': 180, 'left': 270}[fs])
725653
self._path = self._alt_path = half
726654
self._alt_transform = self._transform.frozen().rotate_deg(180.0)
727655

@@ -870,20 +798,10 @@ def _set_plus_filled(self):
870798
self._path = self._plus_filled_path
871799
else:
872800
# 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)
801+
self._path = self._alt_path = self._plus_filled_path_t
802+
self._transform.rotate_deg({
803+
'top': 0, 'left': 90, 'bottom': 180, 'right': 270}[fs])
804+
self._alt_transform = self._transform.frozen().rotate(180)
887805

888806
_x_filled_path = Path(
889807
[(0.25, 0), (0.5, 0.25), (0.75, 0), (1, 0.25), (0.75, 0.5), (1, 0.75),
@@ -902,17 +820,7 @@ def _set_x_filled(self):
902820
self._path = self._x_filled_path
903821
else:
904822
# 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)
823+
self._path = self._alt_path = self._x_filled_path_t
824+
self._transform.rotate_deg({
825+
'top': 0, 'left': 90, 'bottom': 180, 'right': 270}[fs])
826+
self._alt_transform = self._transform.frozen().rotate(180)

0 commit comments

Comments
 (0)