@@ -1951,18 +1951,35 @@ def get_supported_filetypes_grouped(cls):
19511951 groupings [name ].sort ()
19521952 return groupings
19531953
1954- def _get_output_canvas (self , fmt ):
1954+ def _get_output_canvas (self , backend , fmt ):
19551955 """
1956- Return a canvas suitable for saving figures to a specified file format .
1956+ Set the canvas in preparation for saving the figure .
19571957
1958- If necessary, this function will switch to a registered backend that
1959- supports the format.
1960- """
1961- # Return the current canvas if it supports the requested format.
1962- if hasattr (self , 'print_{}' .format (fmt )):
1958+ Parameters
1959+ ----------
1960+ backend : str or None
1961+ If not None, switch the figure canvas to the ``FigureCanvas`` class
1962+ of the given backend.
1963+ fmt : str
1964+ If *backend* is None, then determine a suitable canvas class for
1965+ saving to format *fmt* -- either the current canvas class, if it
1966+ supports *fmt*, or whatever `get_registered_canvas_class` returns;
1967+ switch the figure canvas to that canvas class.
1968+ """
1969+ if backend is not None :
1970+ # Return a specific canvas class, if requested.
1971+ canvas_class = (
1972+ importlib .import_module (cbook ._backend_module_name (backend ))
1973+ .FigureCanvas )
1974+ if not hasattr (canvas_class , f"print_{ fmt } " ):
1975+ raise ValueError (
1976+ f"The { backend !r} backend does not support { fmt } output" )
1977+ elif hasattr (self , f"print_{ fmt } " ):
1978+ # Return the current canvas if it supports the requested format.
19631979 return self
1964- # Return a default canvas for the requested format, if it exists.
1965- canvas_class = get_registered_canvas_class (fmt )
1980+ else :
1981+ # Return a default canvas for the requested format, if it exists.
1982+ canvas_class = get_registered_canvas_class (fmt )
19661983 if canvas_class :
19671984 return self .switch_backends (canvas_class )
19681985 # Else report error for unsupported format.
@@ -1972,7 +1989,7 @@ def _get_output_canvas(self, fmt):
19721989
19731990 def print_figure (self , filename , dpi = None , facecolor = None , edgecolor = None ,
19741991 orientation = 'portrait' , format = None ,
1975- * , bbox_inches = None , ** kwargs ):
1992+ * , bbox_inches = None , backend = None , ** kwargs ):
19761993 """
19771994 Render the figure to hardcopy. Set the figure patch face and edge
19781995 colors. This is useful because some of the GUIs have a gray figure
@@ -2012,6 +2029,13 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20122029 A list of extra artists that will be considered when the
20132030 tight bbox is calculated.
20142031
2032+ backend : str, optional
2033+ Use a non-default backend to render the file, e.g. to render a
2034+ png file with the "cairo" backend rather than the default "agg",
2035+ or a pdf file with the "pgf" backend rather than the default
2036+ "pdf". Note that the default backend is normally sufficient. See
2037+ :ref:`the-builtin-backends` for a list of valid backends for each
2038+ file format. Custom backends can be referenced as "module://...".
20152039 """
20162040 if format is None :
20172041 # get format from filename, or from backend's default filetype
@@ -2026,7 +2050,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20262050 format = format .lower ()
20272051
20282052 # get canvas object and print method for format
2029- canvas = self ._get_output_canvas (format )
2053+ canvas = self ._get_output_canvas (backend , format )
20302054 print_method = getattr (canvas , 'print_%s' % format )
20312055
20322056 if dpi is None :
0 commit comments