From ec68069a96cb41ac287b69a613cb1e70925d09d6 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 28 Aug 2017 12:33:11 -0700 Subject: [PATCH] Fix PdfPages+cairo. ... by forcing pdf backend output from within PdfPages. --- lib/matplotlib/backend_bases.py | 28 ++++++++++++-------------- lib/matplotlib/backends/backend_pdf.py | 23 ++++++++++++--------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index dd540db6eddb..df3337244698 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2081,27 +2081,25 @@ def get_supported_filetypes_grouped(cls): groupings[name].sort() return groupings - def _get_output_canvas(self, format): - """Return a canvas that is suitable for saving figures to a specified - file format. If necessary, this function will switch to a registered - backend that supports the format. + def _get_output_canvas(self, fmt): """ - method_name = 'print_%s' % format + Return a canvas suitable for saving figures to a specified file format. - # check if this canvas supports the requested format + If necessary, this function will switch to a registered backend that + supports the format. + """ + method_name = 'print_%s' % fmt + # Return the current canvas if it supports the requested format. if hasattr(self, method_name): return self - - # check if there is a default canvas for the requested format - canvas_class = get_registered_canvas_class(format) + # Return a default canvas for the requested format, if it exists. + canvas_class = get_registered_canvas_class(fmt) if canvas_class: return self.switch_backends(canvas_class) - - # else report error for unsupported format - formats = sorted(self.get_supported_filetypes()) - raise ValueError('Format "%s" is not supported.\n' - 'Supported formats: ' - '%s.' % (format, ', '.join(formats))) + # Else report error for unsupported format. + raise ValueError( + "Format {!r} is not supported (supported formats: {})" + .format(fmt, ", ".join(sorted(self.get_supported_filetypes())))) def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, orientation='portrait', format=None, **kwargs): diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 87a5c1e64de7..c3c9cdb84281 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2523,18 +2523,21 @@ def savefig(self, figure=None, **kwargs): instance is provided, this figure is saved. If an int is specified, the figure instance to save is looked up by number. """ - if isinstance(figure, Figure): - figure.savefig(self, format='pdf', **kwargs) - else: + if not isinstance(figure, Figure): if figure is None: - figureManager = Gcf.get_active() - else: - figureManager = Gcf.get_fig_manager(figure) - if figureManager is None: - raise ValueError("No such figure: " + repr(figure)) + manager = Gcf.get_active() else: - figureManager.canvas.figure.savefig(self, format='pdf', - **kwargs) + manager = Gcf.get_fig_manager(figure) + if manager is None: + raise ValueError("No figure {}".format(figure)) + figure = manager.canvas.figure + # Force use of pdf backend, as PdfPages is tightly coupled with it. + try: + orig_canvas = figure.canvas + figure.canvas = FigureCanvasPdf(figure) + figure.savefig(self, format="pdf", **kwargs) + finally: + figure.canvas = orig_canvas def get_pagecount(self): """