Skip to content

Commit ec68069

Browse files
committed
Fix PdfPages+cairo.
... by forcing pdf backend output from within PdfPages.
1 parent e3a34c5 commit ec68069

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
@@ -2081,27 +2081,25 @@ def get_supported_filetypes_grouped(cls):
20812081
groupings[name].sort()
20822082
return groupings
20832083

2084-
def _get_output_canvas(self, format):
2085-
"""Return a canvas that is suitable for saving figures to a specified
2086-
file format. If necessary, this function will switch to a registered
2087-
backend that supports the format.
2084+
def _get_output_canvas(self, fmt):
20882085
"""
2089-
method_name = 'print_%s' % format
2086+
Return a canvas suitable for saving figures to a specified file format.
20902087
2091-
# check if this canvas supports the requested format
2088+
If necessary, this function will switch to a registered backend that
2089+
supports the format.
2090+
"""
2091+
method_name = 'print_%s' % fmt
2092+
# Return the current canvas if it supports the requested format.
20922093
if hasattr(self, method_name):
20932094
return self
2094-
2095-
# check if there is a default canvas for the requested format
2096-
canvas_class = get_registered_canvas_class(format)
2095+
# Return a default canvas for the requested format, if it exists.
2096+
canvas_class = get_registered_canvas_class(fmt)
20972097
if canvas_class:
20982098
return self.switch_backends(canvas_class)
2099-
2100-
# else report error for unsupported format
2101-
formats = sorted(self.get_supported_filetypes())
2102-
raise ValueError('Format "%s" is not supported.\n'
2103-
'Supported formats: '
2104-
'%s.' % (format, ', '.join(formats)))
2099+
# Else report error for unsupported format.
2100+
raise ValueError(
2101+
"Format {!r} is not supported (supported formats: {})"
2102+
.format(fmt, ", ".join(sorted(self.get_supported_filetypes()))))
21052103

21062104
def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21072105
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
@@ -2523,18 +2523,21 @@ 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+
orig_canvas = figure.canvas
2537+
figure.canvas = FigureCanvasPdf(figure)
2538+
figure.savefig(self, format="pdf", **kwargs)
2539+
finally:
2540+
figure.canvas = orig_canvas
25382541

25392542
def get_pagecount(self):
25402543
"""

0 commit comments

Comments
 (0)