1212import artist
1313from artist import Artist
1414from cbook import iterable , is_string_like , is_numlike , ls_mapper , dedent ,\
15- flatten
15+ flatten , is_math_text
1616from colors import colorConverter
1717from path import Path
1818from transforms import Affine2D , Bbox , TransformedPath , IdentityTransform
1919
2020from matplotlib import rcParams
2121from artist import allow_rasterization
2222from matplotlib import docstring
23+ from matplotlib .font_manager import FontProperties
2324
2425# special-purpose marker identifiers:
2526(TICKLEFT , TICKRIGHT , TICKUP , TICKDOWN ,
@@ -139,7 +140,7 @@ class Line2D(Artist):
139140 }
140141
141142 filled_markers = ('o' , '^' , 'v' , '<' , '>' ,
142- 's' , 'd' , 'D' , 'h' , 'H' , 'p' , '*' )
143+ 's' , 'd' , 'D' , 'h' , 'H' , 'p' , '*' )
143144
144145 zorder = 2
145146 validCap = ('butt' , 'round' , 'projecting' )
@@ -535,7 +536,7 @@ def draw(self, renderer):
535536 gc .set_foreground (self .get_markeredgecolor ())
536537 gc .set_linewidth (self ._markeredgewidth )
537538 gc .set_alpha (self ._alpha )
538- funcname = self ._markers . get ( self . _marker , '_draw_nothing' )
539+ funcname = self ._markerFunc
539540 if funcname != '_draw_nothing' :
540541 tpath , affine = self ._transformed_path .get_transformed_points_and_affine ()
541542 if len (tpath .vertices ):
@@ -573,7 +574,8 @@ def get_marker(self): return self._marker
573574 def get_markeredgecolor (self ):
574575 if (is_string_like (self ._markeredgecolor ) and
575576 self ._markeredgecolor == 'auto' ):
576- if self ._marker in self .filled_markers :
577+ if (self ._marker in self .filled_markers or
578+ is_math_text (self ._marker )):
577579 return 'k'
578580 else :
579581 return self ._color
@@ -774,6 +776,7 @@ def set_marker(self, marker):
774776 'None' nothing
775777 ' ' nothing
776778 '' nothing
779+ '$...$' render the string using mathtext
777780 ========== ==========================
778781
779782
@@ -782,16 +785,18 @@ def set_marker(self, marker):
782785 | '<' | '>' | 'D' | 'H' | '^' | '_' | 'd'
783786 | 'h' | 'o' | 'p' | 's' | 'v' | 'x' | '|'
784787 | TICKUP | TICKDOWN | TICKLEFT | TICKRIGHT
785- | 'None' | ' ' | '' ]
788+ | 'None' | ' ' | '' | '$...$' ]
786789
787790 """
788- if marker not in self ._markers :
791+ if marker in self ._markers :
792+ self ._marker = marker
793+ self ._markerFunc = self ._markers [marker ]
794+ elif is_math_text (marker ):
795+ self ._marker = marker
796+ self ._markerFunc = '_draw_mathtext_path'
797+ else : #already handle ' ', '' in marker list
789798 verbose .report ('Unrecognized marker style %s, %s' %
790799 (marker , type (marker )))
791- if marker in [' ' ,'' ]:
792- marker = 'None'
793- self ._marker = marker
794- self ._markerFunc = self ._markers [marker ]
795800
796801 def set_markeredgecolor (self , ec ):
797802 """
@@ -867,6 +872,38 @@ def set_dashes(self, seq):
867872 def _draw_lines (self , renderer , gc , path , trans ):
868873 self ._lineFunc (renderer , gc , path , trans )
869874
875+ def _draw_mathtext_path (self , renderer , gc , path , trans ):
876+ """
877+ Draws mathtext markers '$...$' using TextPath object.
878+
879+ Submitted by tcb
880+ """
881+ from matplotlib .patches import PathPatch
882+ from matplotlib .text import TextPath
883+
884+ gc .set_snap (False )
885+
886+ # again, the properties could be initialised just once outside
887+ # this function
888+ # Font size is irrelevant here, it will be rescaled based on
889+ # the drawn size later
890+ props = FontProperties (size = 1.0 )
891+ text = TextPath (xy = (0 ,0 ), s = self .get_marker (), fontproperties = props ,
892+ usetex = rcParams ['text.usetex' ])
893+ if len (text .vertices ) == 0 :
894+ return
895+ xmin , ymin = text .vertices .min (axis = 0 )
896+ xmax , ymax = text .vertices .max (axis = 0 )
897+ width = xmax - xmin
898+ height = ymax - ymin
899+ max_dim = max (width , height )
900+ path_trans = Affine2D () \
901+ .translate (0.5 * - width , 0.5 * - height ) \
902+ .scale ((renderer .points_to_pixels (self .get_markersize ()) / max_dim ))
903+
904+ rgbFace = self ._get_rgb_face ()
905+ renderer .draw_markers (gc , text , path_trans , path , trans , rgbFace )
906+
870907 def _draw_steps_pre (self , renderer , gc , path , trans ):
871908 vertices = self ._xy
872909 steps = ma .zeros ((2 * len (vertices )- 1 , 2 ), np .float_ )
@@ -1288,6 +1325,7 @@ def update_from(self, other):
12881325
12891326 self ._linestyle = other ._linestyle
12901327 self ._marker = other ._marker
1328+ self ._markerFunc = other ._markerFunc
12911329 self ._drawstyle = other ._drawstyle
12921330
12931331
0 commit comments