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'm currently trying to figure out the best way to unify the figures for my thesis, and intend to use the PGF backend to directly\include
figures in my LaTeX document.
When creating PGF figures that include raster graphics (e.g., viaimshow
), the graphic gets stored as a PNG file and imported via\pgfimage
, which is great. Unfortunately, it doesn't supportinterpolation='none'
as the PDF backend does:
importnumpyasnpimportmatplotlibasmplimportmatplotlib.pyplotaspltnp.random.seed(42)data=np.random.randn(5,6)plt.imshow(data,cmap='hot',interpolation='none')plt.savefig('test.pdf')mpl.rcParams['pgf.texsystem']='pdflatex'plt.savefig('test.pgf')
Checking the created PDF file in Evince, it indeed includes a PNG graphic of 5x6 pixels. The graphic for the PGF file is blown up, though:
$ file test-img0.png test-img0.png: PNG image data, 577 x 481, 8-bit/color RGBA, non-interlaced
I can manually fix this by replacing it with a 5x6 PNG file and tweaking the PGF output as follows:
59c59< \pgftext[at=\pgfqpoint{1.220000in}{0.600000in},left,bottom]{\pgfimage[interpolate=true,width=5.770000in,height=4.810000in]{test-img0.png}}%---> \pgftext[at=\pgfqpoint{1.220000in}{0.600000in},left,bottom]{\pgfimage[interpolate=false,width=5.770000in,height=4.810000in]{test-img0.png}}%
Can we modify the PGF backend such that it emits the unscaled raster graphic and usesinterpolate=false
?
I'm willing to work on this myself, but would appreciate some guidance. Looking through the code, the image is written inhttps://github.com/matplotlib/matplotlib/blob/f1bab50/lib/matplotlib/backends/backend_pgf.py#L612.
The corresponding code in the PDF backend starts athttps://github.com/matplotlib/matplotlib/blob/f1bab50/lib/matplotlib/backends/backend_pdf.py#L1600.
It overrides theoption_scale_image()
function to returnTrue
, and it takes an additionaltransform
parameter indraw_image()
. I guess this is where the magic happens -- if there's a transformation to apply, it will get the unmodified image and construct the PDF so the image is scaled appropriately.
The documentation on that parameter is scarce (https://github.com/matplotlib/matplotlib/blob/f1bab50/lib/matplotlib/backend_bases.py#L511), but it seems you can convert it into six values giving an affine transformation.
If I want the PGF backend to handle scaling, will the backend have to handle the full range of possible affine transformations (scaling would suffice for me)? Is thetransform
guaranteed to only be used forinterpolation='none'
, so I can setinterpolate=false
in this case? Is there any better documentation on thetransform
?