Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@mruffalo

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

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