Skip to content

PDFPages Errors on savefig (missing encode) #9112

Closed
@OceanWolf

Description

@OceanWolf

With just a part of http://matplotlib.org/examples/pylab_examples/multipage_pdf.html

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

with PdfPages('multipage_pdf.pdf') as pdf:
    plt.figure(figsize=(3, 3))
    plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
    plt.title('Page One')
    pdf.savefig()  # saves the current figure into a pdf page
    plt.close()

Traceback

pdf.savefig()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_pdf.py", line 2462, in savefig
    figure.savefig(self, format='pdf', **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1572, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2245, in print_figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_cairo.py", line 483, in print_pdf
    return self._save(fobj, 'pdf', *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_cairo.py", line 516, in _save
    surface = cairo.PDFSurface (fo, width_in_points, height_in_points)
  File "/usr/lib/python3/dist-packages/cairocffi/surfaces.py", line 792, in __init__
    _encode_filename(target), width_in_points, height_in_points)
  File "/usr/lib/python3/dist-packages/cairocffi/surfaces.py", line 52, in _encode_filename
    filename = filename.encode(sys.getfilesystemencoding())
AttributeError: 'PdfPages' object has no attribute 'encode'

The issue I see lies in Figure.savefig(self, *args, **kwargs) where Args get listed as:

Arguments:

*fname*:
A string containing a path to a filename, or a Python
file-like object, or possibly some backend-dependent object
such as :class:`~matplotlib.backends.backend_pdf.PdfPages`.

However the stack show above shows this "filename" passed down to cairosurfaces where it wants to encode the filename, which it can't do as it has a PDFPages object and not a filename.

I have been looking through the tests and we don't seem to try to save any tests for this. Maybe however this bug comes up only with the cairocffi.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions