Skip to content

Commit 697f479

Browse files
committed
Fix PdfPages+cairo.
... by forcing pdf backend output from within PdfPages.
1 parent 7cf904d commit 697f479

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,27 +2093,33 @@ def get_supported_filetypes_grouped(cls):
20932093
groupings[name].sort()
20942094
return groupings
20952095

2096-
def _get_output_canvas(self, format):
2097-
"""Return a canvas that is suitable for saving figures to a specified
2098-
file format. If necessary, this function will switch to a registered
2099-
backend that supports the format.
2096+
def _get_output_canvas(self, fmt):
21002097
"""
2101-
method_name = 'print_%s' % format
2098+
Return a canvas suitable for saving figures to a specified file format.
21022099
2103-
# check if this canvas supports the requested format
2104-
if hasattr(self, method_name):
2100+
If necessary, this function will switch to a registered backend that
2101+
supports the format.
2102+
"""
2103+
method_name = 'print_%s' % fmt
2104+
2105+
# Return the current canvas if it supports the requested format. Note
2106+
# that we explicitly support setting print_foo to None to remove
2107+
# support for a format; this is required for PdfPages to work while the
2108+
# current backend supports pdf output (because PdfPages is tightly
2109+
# coupled with backend_pdf). This feature may be removed if PdfPages
2110+
# gets decoupled from backend_pdf.
2111+
if getattr(self, method_name, None) is not None:
21052112
return self
21062113

2107-
# check if there is a default canvas for the requested format
2108-
canvas_class = get_registered_canvas_class(format)
2114+
# Return a default canvas for the requested format, if it exists.
2115+
canvas_class = get_registered_canvas_class(fmt)
21092116
if canvas_class:
21102117
return self.switch_backends(canvas_class)
21112118

2112-
# else report error for unsupported format
2113-
formats = sorted(self.get_supported_filetypes())
2114-
raise ValueError('Format "%s" is not supported.\n'
2115-
'Supported formats: '
2116-
'%s.' % (format, ', '.join(formats)))
2119+
# Else report error for unsupported format.
2120+
raise ValueError(
2121+
"Format {!r} is not supported (supported formats: {})"
2122+
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
21172123

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

lib/matplotlib/backends/backend_pdf.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,18 +2523,20 @@ def savefig(self, figure=None, **kwargs):
25232523
instance is provided, this figure is saved. If an int is specified,
25242524
the figure instance to save is looked up by number.
25252525
"""
2526-
if isinstance(figure, Figure):
2527-
figure.savefig(self, format='pdf', **kwargs)
2528-
else:
2526+
if not isinstance(figure, Figure):
25292527
if figure is None:
2530-
figureManager = Gcf.get_active()
2531-
else:
2532-
figureManager = Gcf.get_fig_manager(figure)
2533-
if figureManager is None:
2534-
raise ValueError("No such figure: " + repr(figure))
2528+
manager = Gcf.get_active()
25352529
else:
2536-
figureManager.canvas.figure.savefig(self, format='pdf',
2537-
**kwargs)
2530+
manager = Gcf.get_fig_manager(figure)
2531+
if manager is None:
2532+
raise ValueError("No figure {}".format(figure))
2533+
figure = manager.canvas.figure
2534+
# Force use of pdf backend, as PdfPages is tightly coupled with it.
2535+
try:
2536+
figure.canvas = FigureCanvasPdf(figure)
2537+
figure.savefig(self, format="pdf", **kwargs)
2538+
finally:
2539+
figure.canvas = manager.canvas
25382540

25392541
def get_pagecount(self):
25402542
"""

0 commit comments

Comments
 (0)