Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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 likedpi ='raw'
(for example) would simply do what I explained before.
Even when explicitely addingresample = 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.
importnumpyasnpimportmatplotlibmatplotlib.use("agg")importmatplotlib.pyplotaspltdefadd_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 withthis image from wikipedia asimage.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