17
17
_Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
18
18
RendererBase )
19
19
from matplotlib .backends .backend_mixed import MixedModeRenderer
20
- from matplotlib .cbook import is_writable_file_like
21
20
from matplotlib .path import Path
22
21
from matplotlib .figure import Figure
23
22
from matplotlib ._pylab_helpers import Gcf
@@ -379,8 +378,8 @@ def __init__(self, figure, fh, dummy=False):
379
378
Matplotlib figure to initialize height, width and dpi from.
380
379
fh : file-like
381
380
File handle for the output of the drawing commands.
382
-
383
381
"""
382
+
384
383
RendererBase .__init__ (self )
385
384
self .dpi = figure .dpi
386
385
self .fh = fh
@@ -836,16 +835,10 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
836
835
if kwargs .get ("dryrun" , False ):
837
836
self ._print_pgf_to_fh (None , * args , ** kwargs )
838
837
return
839
-
840
- # figure out where the pgf is to be written to
841
- if isinstance (fname_or_fh , str ):
842
- with open (fname_or_fh , "w" , encoding = "utf-8" ) as fh :
843
- self ._print_pgf_to_fh (fh , * args , ** kwargs )
844
- elif is_writable_file_like (fname_or_fh ):
845
- fh = codecs .getwriter ("utf-8" )(fname_or_fh )
846
- self ._print_pgf_to_fh (fh , * args , ** kwargs )
847
- else :
848
- raise ValueError ("filename must be a path" )
838
+ with cbook .open_file_cm (fname_or_fh , "w" , encoding = "utf-8" ) as file :
839
+ if not cbook .file_requires_unicode (file ):
840
+ file = codecs .getwriter ("utf-8" )(file )
841
+ self ._print_pgf_to_fh (file , * args , ** kwargs )
849
842
850
843
def _print_pdf_to_fh (self , fh , * args , ** kwargs ):
851
844
w , h = self .figure .get_figwidth (), self .figure .get_figheight ()
@@ -890,21 +883,12 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
890
883
TmpDirCleaner .add (tmpdir )
891
884
892
885
def print_pdf (self , fname_or_fh , * args , ** kwargs ):
893
- """
894
- Use LaTeX to compile a Pgf generated figure to PDF.
895
- """
886
+ """Use LaTeX to compile a Pgf generated figure to PDF."""
896
887
if kwargs .get ("dryrun" , False ):
897
888
self ._print_pgf_to_fh (None , * args , ** kwargs )
898
889
return
899
-
900
- # figure out where the pdf is to be written to
901
- if isinstance (fname_or_fh , str ):
902
- with open (fname_or_fh , "wb" ) as fh :
903
- self ._print_pdf_to_fh (fh , * args , ** kwargs )
904
- elif is_writable_file_like (fname_or_fh ):
905
- self ._print_pdf_to_fh (fname_or_fh , * args , ** kwargs )
906
- else :
907
- raise ValueError ("filename must be a path or a file-like object" )
890
+ with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
891
+ self ._print_pdf_to_fh (file , * args , ** kwargs )
908
892
909
893
def _print_png_to_fh (self , fh , * args , ** kwargs ):
910
894
converter = make_pdf_to_png_converter ()
@@ -926,21 +910,13 @@ def _print_png_to_fh(self, fh, *args, **kwargs):
926
910
except :
927
911
TmpDirCleaner .add (tmpdir )
928
912
929
- def print_png (self , fname_or_fh , * args , ** kwargs ):
930
- """
931
- Use LaTeX to compile a pgf figure to pdf and convert it to png.
932
- """
913
+ def print_png (self , fname_or_fh , * args , dryrun = False , ** kwargs ):
914
+ """Use LaTeX to compile a pgf figure to pdf and convert it to png."""
933
915
if kwargs .get ("dryrun" , False ):
934
916
self ._print_pgf_to_fh (None , * args , ** kwargs )
935
917
return
936
-
937
- if isinstance (fname_or_fh , str ):
938
- with open (fname_or_fh , "wb" ) as fh :
939
- self ._print_png_to_fh (fh , * args , ** kwargs )
940
- elif is_writable_file_like (fname_or_fh ):
941
- self ._print_png_to_fh (fname_or_fh , * args , ** kwargs )
942
- else :
943
- raise ValueError ("filename must be a path or a file-like object" )
918
+ with cbook .open_file_cm (fname_or_fh , "wb" ) as file :
919
+ self ._print_png_to_fh (file , * args , ** kwargs )
944
920
945
921
def get_renderer (self ):
946
922
return RendererPgf (self .figure , None , dummy = True )
0 commit comments