@@ -337,19 +337,19 @@ def get_width_height_descent(self, text, prop):
337337
338338class RendererPgf (RendererBase ):
339339
340- def __init__ (self , figure , fh , draw_texts = True ):
340+ def __init__ (self , figure , fh ):
341341 """
342- Creates a new Pgf renderer that translates any drawing instruction
343- into commands to be interpreted in a latex pgfpicture environment.
342+ Creates a new PGF renderer that translates any drawing instruction
343+ into text commands to be interpreted in a latex pgfpicture environment.
344344
345- If `draw_texts` is False, the draw_text calls are ignored and the
346- text elements must be rendered in a different way.
345+ Attributes:
346+ * figure: Matplotlib figure to initialize height, width and dpi from.
347+ * fh: File handle for the output of the drawing commands.
347348 """
348349 RendererBase .__init__ (self )
349350 self .dpi = figure .dpi
350351 self .fh = fh
351352 self .figure = figure
352- self .draw_texts = draw_texts
353353 self .image_counter = 0
354354
355355 # get LatexManager instance
@@ -542,8 +542,6 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!"):
542542 self .draw_text (gc , x , y , s , prop , angle , ismath )
543543
544544 def draw_text (self , gc , x , y , s , prop , angle , ismath = False ):
545- if not self .draw_texts : return
546-
547545 s = common_texification (s )
548546
549547 # apply font properties
@@ -631,27 +629,47 @@ def print_pgf(self, filename, *args, **kwargs):
631629 rendered in latex documents.
632630 """
633631
634- header_text = r"""%% Pgf figure exported from matplotlib.
632+ header_text = r"""%% Creator: Matplotlib, PGF backend
635633%%
636- %% To include the image in your LaTeX document, write
634+ %% To include the figure in your LaTeX document, write
637635%% \input{<filename>.pgf}
638636%%
639- %% Make sure to load the required packages in your main document
637+ %% Make sure the required packages are loaded in your preamble
640638%% \usepackage{pgf}
641- %"""
642- # figure size in units of in
639+ %%
640+ %% Figures using additional raster images can only be included by \input if
641+ %% they are in the same directory as the main LaTeX file. For loading figures
642+ %% from other directories you can use the `import` package
643+ %% \usepackage{import}
644+ %% and then include the figures with
645+ %% \import{<path to file>}{<filename>.pgf}
646+ %%
647+ """
648+
649+ # append the preamble used by the backend as a comment for debugging
650+ header_info_preamble = ["%% Matplotlib used the following preamble" ]
651+ for line in get_preamble ().splitlines ():
652+ header_info_preamble .append ("%% " + line )
653+ for line in get_fontspec ().splitlines ():
654+ header_info_preamble .append ("%% " + line )
655+ header_info_preamble .append ("%%" )
656+ header_info_preamble = "\n " .join (header_info_preamble )
657+
658+ # get figure size in inch
643659 w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
644660
645661 # start a pgfpicture environment and set a bounding box
646662 fh = codecs .open (filename , "wt" , encoding = "utf-8" )
647- writeln (fh , header_text )
663+ fh .write (header_text )
664+ fh .write (header_info_preamble )
665+ fh .write ("\n " )
648666 writeln (fh , r"\begingroup" )
649667 writeln (fh , r"\makeatletter" )
650668 writeln (fh , r"\begin{pgfpicture}" )
651669 writeln (fh , r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}" % (w ,h ))
652670 writeln (fh , r"\pgfusepath{use as bounding box}" )
653671
654- renderer = RendererPgf (self .figure , fh , draw_texts = True )
672+ renderer = RendererPgf (self .figure , fh )
655673 self .figure .draw (renderer )
656674
657675 # end the pgfpicture environment
@@ -766,7 +784,7 @@ def _render_texts_pgf(self, fh):
766784 writeln (fh , ur"\pgftext[%s,x=%fin,y=%fin,rotate=%f]{%s}" % (align ,x ,y ,angle ,s ))
767785
768786 def get_renderer (self ):
769- return RendererPgf (self .figure , None , draw_texts = False )
787+ return RendererPgf (self .figure , None )
770788
771789class FigureManagerPgf (FigureManagerBase ):
772790 def __init__ (self , * args ):
0 commit comments