Skip to content

Commit 206b5fe

Browse files
authored
Merge pull request #9114 from anntzer/pdfpages-cairo
FIX: PdfPages+cairo.
2 parents 33bd5ed + ec68069 commit 206b5fe

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,27 +2084,25 @@ def get_supported_filetypes_grouped(cls):
20842084
groupings[name].sort()
20852085
return groupings
20862086

2087-
def _get_output_canvas(self, format):
2088-
"""Return a canvas that is suitable for saving figures to a specified
2089-
file format. If necessary, this function will switch to a registered
2090-
backend that supports the format.
2087+
def _get_output_canvas(self, fmt):
20912088
"""
2092-
method_name = 'print_%s' % format
2089+
Return a canvas suitable for saving figures to a specified file format.
20932090
2094-
# check if this canvas supports the requested format
2091+
If necessary, this function will switch to a registered backend that
2092+
supports the format.
2093+
"""
2094+
method_name = 'print_%s' % fmt
2095+
# Return the current canvas if it supports the requested format.
20952096
if hasattr(self, method_name):
20962097
return self
2097-
2098-
# check if there is a default canvas for the requested format
2099-
canvas_class = get_registered_canvas_class(format)
2098+
# Return a default canvas for the requested format, if it exists.
2099+
canvas_class = get_registered_canvas_class(fmt)
21002100
if canvas_class:
21012101
return self.switch_backends(canvas_class)
2102-
2103-
# else report error for unsupported format
2104-
formats = sorted(self.get_supported_filetypes())
2105-
raise ValueError('Format "%s" is not supported.\n'
2106-
'Supported formats: '
2107-
'%s.' % (format, ', '.join(formats)))
2102+
# Else report error for unsupported format.
2103+
raise ValueError(
2104+
"Format {!r} is not supported (supported formats: {})"
2105+
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
21082106

21092107
def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21102108
orientation='portrait', format=None, **kwargs):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,18 +2514,21 @@ def savefig(self, figure=None, **kwargs):
25142514
instance is provided, this figure is saved. If an int is specified,
25152515
the figure instance to save is looked up by number.
25162516
"""
2517-
if isinstance(figure, Figure):
2518-
figure.savefig(self, format='pdf', **kwargs)
2519-
else:
2517+
if not isinstance(figure, Figure):
25202518
if figure is None:
2521-
figureManager = Gcf.get_active()
2522-
else:
2523-
figureManager = Gcf.get_fig_manager(figure)
2524-
if figureManager is None:
2525-
raise ValueError("No such figure: " + repr(figure))
2519+
manager = Gcf.get_active()
25262520
else:
2527-
figureManager.canvas.figure.savefig(self, format='pdf',
2528-
**kwargs)
2521+
manager = Gcf.get_fig_manager(figure)
2522+
if manager is None:
2523+
raise ValueError("No figure {}".format(figure))
2524+
figure = manager.canvas.figure
2525+
# Force use of pdf backend, as PdfPages is tightly coupled with it.
2526+
try:
2527+
orig_canvas = figure.canvas
2528+
figure.canvas = FigureCanvasPdf(figure)
2529+
figure.savefig(self, format="pdf", **kwargs)
2530+
finally:
2531+
figure.canvas = orig_canvas
25292532

25302533
def get_pagecount(self):
25312534
"""

0 commit comments

Comments
 (0)