Skip to content

PDF saved as raster graphic when plt.savefig is given a Path object #10753

Closed

Description

@mruffalo

Bug report

I use pathlib quite heavily, and just updated my copy of matplotlib to 2.2.0. I was happy to remove lots of explicit conversion from Path to str in my code, e.g. plt.savefig(str(figure_path)) -> plt.savefig(figure_path).

When passing a pathlib.Path object to plt.savefig, I was surprised to see that the plot was saved as a low-resolution raster image in the PDF, rather than a vector graphic.

Code for reproduction

from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.exp(x)
plt.plot(x, y)

plt.savefig('str_path.pdf')
plt.savefig(Path('pathlib_path.pdf'))

Actual outcome
str_path.pdf
pathlib_path.pdf

Expected outcome

A vector graphic would be saved when using both str paths and pathlib.Path objects.

Matplotlib version

  • Operating system: Kubuntu 16.04
  • Matplotlib version: 2.2.0, installed via pip, using pre-built binary wheel
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg (though I assume that doesn't apply when saving as PDF)
  • Python version: 3.6.4
  • Jupyter version (if applicable):
  • Other libraries: NumPy 1.14.1 used in reproducible example code

Activity

  1. jklymak commented on Mar 11, 2018

    @jklymak
    Member

    Ping @anntzer. I suspect that we can’t tell this is meant to be a PDF. I think there is a kind kwarg.

  2. QuLogic commented on Mar 11, 2018

    @QuLogic
    Member

    Yep, they're not the same format:

    $ file *path.pdf
    pathlib_path.pdf: PNG image data, 640 x 480, 8-bit/color RGBA, non-interlaced
    str_path.pdf:     PDF document, version 1.4
    

    You can pass format='pdf' to be explicit, though it would be great if this could still be figured out from the Path.

  3. added this to the v2.2.1 milestone on Mar 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions