13
13
import matplotlib as mpl
14
14
from matplotlib .backend_bases import RendererBase , GraphicsContextBase ,\
15
15
FigureManagerBase , FigureCanvasBase
16
+ from matplotlib .backends .backend_mixed import MixedModeRenderer
16
17
from matplotlib .figure import Figure
17
18
from matplotlib .text import Text
18
19
from matplotlib .path import Path
@@ -738,7 +739,7 @@ def __init__(self, *args):
738
739
def get_default_filetype (self ):
739
740
return 'pdf'
740
741
741
- def _print_pgf_to_fh (self , fh ):
742
+ def _print_pgf_to_fh (self , fh , * args , ** kwargs ):
742
743
header_text = r"""%% Creator: Matplotlib, PGF backend
743
744
%%
744
745
%% To include the figure in your LaTeX document, write
@@ -767,6 +768,7 @@ def _print_pgf_to_fh(self, fh):
767
768
768
769
# get figure size in inch
769
770
w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
771
+ dpi = self .figure .get_dpi ()
770
772
771
773
# create pgfpicture environment and write the pgf code
772
774
fh .write (header_text )
@@ -777,7 +779,10 @@ def _print_pgf_to_fh(self, fh):
777
779
writeln (fh , r"\begin{pgfpicture}" )
778
780
writeln (fh , r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}" % (w , h ))
779
781
writeln (fh , r"\pgfusepath{use as bounding box}" )
780
- renderer = RendererPgf (self .figure , fh )
782
+ _bbox_inches_restore = kwargs .pop ("bbox_inches_restore" , None )
783
+ renderer = MixedModeRenderer (self .figure , w , h , dpi ,
784
+ RendererPgf (self .figure , fh ),
785
+ bbox_inches_restore = _bbox_inches_restore )
781
786
self .figure .draw (renderer )
782
787
783
788
# end the pgfpicture environment
@@ -796,14 +801,14 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
796
801
# figure out where the pgf is to be written to
797
802
if is_string_like (fname_or_fh ):
798
803
with codecs .open (fname_or_fh , "w" , encoding = "utf-8" ) as fh :
799
- self ._print_pgf_to_fh (fh )
804
+ self ._print_pgf_to_fh (fh , * args , ** kwargs )
800
805
elif is_writable_file_like (fname_or_fh ):
801
806
raise ValueError ("saving pgf to a stream is not supported, " +
802
807
"consider using the pdf option of the pgf-backend" )
803
808
else :
804
809
raise ValueError ("filename must be a path" )
805
810
806
- def _print_pdf_to_fh (self , fh ):
811
+ def _print_pdf_to_fh (self , fh , * args , ** kwargs ):
807
812
w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
808
813
809
814
try :
@@ -814,7 +819,7 @@ def _print_pdf_to_fh(self, fh):
814
819
fname_pdf = os .path .join (tmpdir , "figure.pdf" )
815
820
816
821
# print figure to pgf and compile it with latex
817
- self .print_pgf (fname_pgf )
822
+ self .print_pgf (fname_pgf , * args , ** kwargs )
818
823
819
824
latex_preamble = get_preamble ()
820
825
latex_fontspec = get_fontspec ()
@@ -856,13 +861,13 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
856
861
# figure out where the pdf is to be written to
857
862
if is_string_like (fname_or_fh ):
858
863
with open (fname_or_fh , "wb" ) as fh :
859
- self ._print_pdf_to_fh (fh )
864
+ self ._print_pdf_to_fh (fh , * args , ** kwargs )
860
865
elif is_writable_file_like (fname_or_fh ):
861
- self ._print_pdf_to_fh (fname_or_fh )
866
+ self ._print_pdf_to_fh (fname_or_fh , * args , ** kwargs )
862
867
else :
863
868
raise ValueError ("filename must be a path or a file-like object" )
864
869
865
- def _print_png_to_fh (self , fh ):
870
+ def _print_png_to_fh (self , fh , * args , ** kwargs ):
866
871
converter = make_pdf_to_png_converter ()
867
872
868
873
try :
@@ -871,7 +876,7 @@ def _print_png_to_fh(self, fh):
871
876
fname_pdf = os .path .join (tmpdir , "figure.pdf" )
872
877
fname_png = os .path .join (tmpdir , "figure.png" )
873
878
# create pdf and try to convert it to png
874
- self .print_pdf (fname_pdf )
879
+ self .print_pdf (fname_pdf , * args , ** kwargs )
875
880
converter (fname_pdf , fname_png , dpi = self .figure .dpi )
876
881
# copy file contents to target
877
882
with open (fname_png , "rb" ) as fh_src :
@@ -888,9 +893,9 @@ def print_png(self, fname_or_fh, *args, **kwargs):
888
893
"""
889
894
if is_string_like (fname_or_fh ):
890
895
with open (fname_or_fh , "wb" ) as fh :
891
- self ._print_png_to_fh (fh )
896
+ self ._print_png_to_fh (fh , * args , ** kwargs )
892
897
elif is_writable_file_like (fname_or_fh ):
893
- self ._print_png_to_fh (fname_or_fh )
898
+ self ._print_png_to_fh (fname_or_fh , * args , ** kwargs )
894
899
else :
895
900
raise ValueError ("filename must be a path or a file-like object" )
896
901
0 commit comments