Open
Description
I sometimes annotate plots using images (png format), and I want the image data not to be touched when saving to pdf: just put the data and the correct transformation in the pdf, if possible.
So instead of having a "fixed" dpi (which does not really make sense for vector formats anyway), passing something like dpi ='raw'
(for example) would simply do what I explained before.
Even when explicitely adding resample = False
, matplotlib still resample the image (I know this value is the default, but still). I don't know if this is a bug report or a feature request, I'm enclined to the former.
import numpy as np
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
def add_image(axe, filename, position, zoom):
img = plt.imread(filename)
off_img = matplotlib.offsetbox.OffsetImage(img, zoom = zoom, resample = False)
art = matplotlib.offsetbox.AnnotationBbox(off_img, position, xybox = (0, 0),
xycoords = axe.transAxes, boxcoords = "offset points", frameon = False)
axe.add_artist(art)
# ==========
fig = plt.figure()
axe = plt.axes()
fig.set_size_inches(3, 1.5)
axe.plot(np.arange(10), np.arange(10))
add_image(axe, "image.png", position = (0.2, 0.7), zoom = 0.07)
fig.savefig("temp.pdf", bbox_inches = "tight", pad_inches = 0)
Use it with this image from wikipedia as image.png
.
Actual outcome (screenshot):
Expected outcome (screenshot):
Matplotlib:
- Version: 3.1.2
- Backend: PDF
- Python: 3.7 64bit
- Installed via:
pip
- OS: Windows 10
Thank you