@@ -599,26 +599,49 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
599
599
self .draw_text (gc , x , y , s , prop , angle , ismath , mtext )
600
600
601
601
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
602
+ # prepare string for tex
602
603
s = common_texification (s )
603
-
604
- # apply font properties
605
604
prop_cmds = _font_properties_str (prop )
606
605
s = ur"{%s %s}" % (prop_cmds , s )
607
606
608
- # draw text at given coordinates
609
- x = x * 1. / self .dpi
610
- y = y * 1. / self .dpi
607
+
611
608
writeln (self .fh , r"\begin{pgfscope}" )
609
+
612
610
alpha = gc .get_alpha ()
613
611
if alpha != 1.0 :
614
612
writeln (self .fh , r"\pgfsetfillopacity{%f}" % alpha )
615
613
writeln (self .fh , r"\pgfsetstrokeopacity{%f}" % alpha )
616
- stroke_rgb = tuple (gc .get_rgb ())[:3 ]
617
- if stroke_rgb != (0 , 0 , 0 ):
618
- writeln (self .fh , r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % stroke_rgb )
614
+ rgb = tuple (gc .get_rgb ())[:3 ]
615
+ if rgb != (0 , 0 , 0 ):
616
+ writeln (self .fh , r"\definecolor{textcolor}{rgb}{%f,%f,%f}" % rgb )
619
617
writeln (self .fh , r"\pgfsetstrokecolor{textcolor}" )
620
618
writeln (self .fh , r"\pgfsetfillcolor{textcolor}" )
621
- writeln (self .fh , "\\ pgftext[left,bottom,x=%fin,y=%fin,rotate=%f]{%s}\n " % (x , y , angle , s ))
619
+
620
+ f = 1.0 / self .figure .dpi
621
+ text_args = []
622
+ if angle == 0 or mtext .get_rotation_mode () == "anchor" :
623
+ # if text anchoring can be supported, get the original coordinates
624
+ # and add alignment information
625
+ x , y = mtext .get_transform ().transform_point (mtext .get_position ())
626
+ text_args .append ("x=%fin" % (x * f ))
627
+ text_args .append ("y=%fin" % (y * f ))
628
+
629
+ halign = {"left" : "left" , "right" : "right" , "center" : "" }
630
+ valign = {"top" : "top" , "bottom" : "bottom" ,
631
+ "baseline" : "base" , "center" : "" }
632
+ text_args .append (halign [mtext .get_ha ()])
633
+ text_args .append (valign [mtext .get_va ()])
634
+ else :
635
+ # if not, use the text layout provided by matplotlib
636
+ text_args .append ("x=%fin" % (x * f ))
637
+ text_args .append ("y=%fin" % (y * f ))
638
+ text_args .append ("left" )
639
+ text_args .append ("bottom" )
640
+
641
+ if angle != 0 :
642
+ text_args .append ("rotate=%f" % angle )
643
+
644
+ writeln (self .fh , r"\pgftext[%s]{%s}" % ("," .join (text_args ), s ))
622
645
writeln (self .fh , r"\end{pgfscope}" )
623
646
624
647
def get_text_width_height_descent (self , s , prop , ismath ):
@@ -861,53 +884,6 @@ def print_png(self, fname_or_fh, *args, **kwargs):
861
884
else :
862
885
raise ValueError ("filename must be a path or a file-like object" )
863
886
864
- def _render_texts_pgf (self , fh ):
865
- # TODO: currently unused code path
866
-
867
- # alignment anchors
868
- valign = {"top" : "top" , "bottom" : "bottom" , "baseline" : "base" , "center" : "" }
869
- halign = {"left" : "left" , "right" : "right" , "center" : "" }
870
- # alignment anchors for 90deg. rotated labels
871
- rvalign = {"top" : "left" , "bottom" : "right" , "baseline" : "right" , "center" : "" }
872
- rhalign = {"left" : "top" , "right" : "bottom" , "center" : "" }
873
-
874
- # TODO: matplotlib does not hide unused tick labels yet, workaround
875
- for tick in self .figure .findobj (mpl .axis .Tick ):
876
- tick .label1 .set_visible (tick .label1On )
877
- tick .label2 .set_visible (tick .label2On )
878
- # TODO: strange, first legend label is always "None", workaround
879
- for legend in self .figure .findobj (mpl .legend .Legend ):
880
- labels = legend .findobj (mpl .text .Text )
881
- labels [0 ].set_visible (False )
882
- # TODO: strange, legend child labels are duplicated,
883
- # find a list of unique text objects as workaround
884
- texts = self .figure .findobj (match = Text , include_self = False )
885
- texts = list (set (texts ))
886
-
887
- # draw text elements
888
- for text in texts :
889
- s = text .get_text ()
890
- if not s or not text .get_visible ():
891
- continue
892
-
893
- s = common_texification (s )
894
-
895
- fontsize = text .get_fontsize ()
896
- angle = text .get_rotation ()
897
- transform = text .get_transform ()
898
- x , y = transform .transform_point (text .get_position ())
899
- x = x * 1.0 / self .figure .dpi
900
- y = y * 1.0 / self .figure .dpi
901
- # TODO: positioning behavior unknown for rotated elements
902
- # right now only the alignment for 90deg rotations is correct
903
- if angle == 90. :
904
- align = rvalign [text .get_va ()] + "," + rhalign [text .get_ha ()]
905
- else :
906
- align = valign [text .get_va ()] + "," + halign [text .get_ha ()]
907
-
908
- s = ur"{\fontsize{%f}{%f}\selectfont %s}" % (fontsize , fontsize * 1.2 , s )
909
- writeln (fh , ur"\pgftext[%s,x=%fin,y=%fin,rotate=%f]{%s}" % (align ,x ,y ,angle ,s ))
910
-
911
887
def get_renderer (self ):
912
888
return RendererPgf (self .figure , None )
913
889
0 commit comments