44"""
55
66from __future__ import division
7- import sys , warnings
7+ import os , sys , warnings
88
99import numpy as npy
1010import matplotlib .numerix .npyma as ma
1111import matplotlib .cbook as cbook
1212import matplotlib .colors as colors
1313import matplotlib .transforms as transforms
1414import matplotlib .widgets as widgets
15+ from matplotlib import rcParams
1516
1617class RendererBase :
1718 """An abstract base class to handle drawing/rendering operations
@@ -1071,8 +1072,70 @@ def get_width_height(self):
10711072 (depending on the backend), truncated to integers"""
10721073 return int (self .figure .bbox .width ()), int (self .figure .bbox .height ())
10731074
1075+ filetypes = {
1076+ 'emf' : 'Enhanced Metafile' ,
1077+ 'eps' : 'Encapsulated Postscript' ,
1078+ 'pdf' : 'Portable Document Format' ,
1079+ 'png' : 'Portable Network Graphics' ,
1080+ 'ps' : 'Postscript' ,
1081+ 'raw' : 'Raw RGBA bitmap' ,
1082+ 'rgb' : 'Raw RGBA bitmap' ,
1083+ 'svg' : 'Scalable Vector Graphics' ,
1084+ }
1085+
1086+ # All of these print_* functions do a lazy import because
1087+ # a) otherwise we'd have cyclical imports, since all of these
1088+ # classes inherit from FigureCanvasBase
1089+ # b) so we don't import a bunch of stuff the user may never use
1090+
1091+ def print_emf (self , * args , ** kwargs ):
1092+ from backends .backend_emf import FigureCanvasEMF # lazy import
1093+ emf = self .switch_backends (FigureCanvasEMF )
1094+ return emf .print_emf (* args , ** kwargs )
1095+
1096+ def print_eps (self , * args , ** kwargs ):
1097+ from backends .backend_ps import FigureCanvasPS # lazy import
1098+ ps = self .switch_backends (FigureCanvasPS )
1099+ return ps .print_eps (* args , ** kwargs )
1100+
1101+ def print_pdf (self , * args , ** kwargs ):
1102+ from backends .backend_pdf import FigureCanvasPdf # lazy import
1103+ pdf = self .switch_backends (FigureCanvasPdf )
1104+ return pdf .print_pdf (* args , ** kwargs )
1105+
1106+ def print_png (self , * args , ** kwargs ):
1107+ from backends .backend_agg import FigureCanvasAgg # lazy import
1108+ agg = self .switch_backends (FigureCanvasAgg )
1109+ return agg .print_png (* args , ** kwargs )
1110+
1111+ def print_ps (self , * args , ** kwargs ):
1112+ from backends .backend_ps import FigureCanvasPS # lazy import
1113+ ps = self .switch_backends (FigureCanvasPS )
1114+ return ps .print_ps (* args , ** kwargs )
1115+
1116+ def print_raw (self , * args , ** kwargs ):
1117+ from backends .backend_agg import FigureCanvasAgg # lazy import
1118+ agg = self .switch_backends (FigureCanvasAgg )
1119+ return agg .print_raw (* args , ** kwargs )
1120+ print_bmp = print_rgb = print_raw
1121+
1122+ def print_svg (self , * args , ** kwargs ):
1123+ from backends .backend_svg import FigureCanvasSVG # lazy import
1124+ svg = self .switch_backends (FigureCanvasSVG )
1125+ return svg .print_svg (* args , ** kwargs )
1126+
1127+ def get_supported_filetypes (self ):
1128+ return self .filetypes
1129+
1130+ def get_supported_filetypes_grouped (self ):
1131+ groupings = {}
1132+ for ext , name in self .filetypes .items ():
1133+ groupings .setdefault (name , []).append (ext )
1134+ groupings [name ].sort ()
1135+ return groupings
1136+
10741137 def print_figure (self , filename , dpi = None , facecolor = 'w' , edgecolor = 'w' ,
1075- orientation = 'portrait' , ** kwargs ):
1138+ orientation = 'portrait' , format = None , ** kwargs ):
10761139 """
10771140 Render the figure to hardcopy. Set the figure patch face and edge
10781141 colors. This is useful because some of the GUIs have a gray figure
@@ -1085,9 +1148,57 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
10851148 facecolor - the facecolor of the figure
10861149 edgecolor - the edgecolor of the figure
10871150 orientation - 'landscape' | 'portrait' (not supported on all backends)
1088- """
1089- pass
1151+ format - when set, forcibly set the file format to save to
1152+ """
1153+ if format is None :
1154+ if cbook .is_string_like (filename ):
1155+ format = os .path .splitext (filename )[1 ][1 :]
1156+ if format is None or format == '' :
1157+ format = self .get_default_filetype ()
1158+ if cbook .is_string_like (filename ):
1159+ filename = filename .rstrip ('.' ) + '.' + format
1160+ format = format .lower ()
1161+
1162+ method_name = 'print_%s' % format
1163+ if (format not in self .filetypes or
1164+ not hasattr (self , method_name )):
1165+ formats = self .filetypes .keys ()
1166+ formats .sort ()
1167+ raise ValueError (
1168+ 'Format "%s" is not supported.\n '
1169+ 'Supported formats: '
1170+ '%s.' % (format , ', ' .join (formats )))
1171+
1172+ if dpi is None :
1173+ dpi = rcParams ['savefig.dpi' ]
1174+
1175+ origDPI = self .figure .dpi .get ()
1176+ origfacecolor = self .figure .get_facecolor ()
1177+ origedgecolor = self .figure .get_edgecolor ()
1178+
1179+ self .figure .dpi .set (dpi )
1180+ self .figure .set_facecolor (facecolor )
1181+ self .figure .set_edgecolor (edgecolor )
10901182
1183+ try :
1184+ result = getattr (self , method_name )(
1185+ filename ,
1186+ dpi = dpi ,
1187+ facecolor = facecolor ,
1188+ edgecolor = edgecolor ,
1189+ orientation = orientation ,
1190+ ** kwargs )
1191+ finally :
1192+ self .figure .dpi .set (origDPI )
1193+ self .figure .set_facecolor (origfacecolor )
1194+ self .figure .set_edgecolor (origedgecolor )
1195+ self .figure .set_canvas (self )
1196+
1197+ return result
1198+
1199+ def get_default_filetype (self ):
1200+ raise NotImplementedError
1201+
10911202 def switch_backends (self , FigureCanvasClass ):
10921203 """
10931204 instantiate an instance of FigureCanvasClass
0 commit comments