[Matplotlib-users] Why is canvas.print_figure to a png reduce the real size of my image

Jody Klymak jklymak at uvic.ca
Fri Oct 20 17:59:59 EDT 2017


Hi David,

What are you trying to achieve as the output?  An image with no white space?  

`bbox_inches=tight` changes the size of the figure to get rid of white space around it.  Prob not what you really want.

Drilling down into `canvas` shouldn’t really be necessary. 

What is `PLB`?  etc?

This will get you pretty close if you just want an image w/ no white space.  But I’m not 100% sure that every pixel has been preserved…


```
import matplotlib.pyplot as plt
import numpy as np

im = np.floor(np.random.rand(3601,2401)*100)

fig, ax = plt.subplots(figsize = (2401/300, 3601/300), dpi=300)

# turn off axis and labels
ax.set_axis_off()
# fill the figure
ax.set_position([0., 0., 1., 1.])
ax.imshow(im)
fig.savefig('Test.png', dpi=300)
```




> On 20 Oct 2017, at  14:38 PM, David Goldsmith <eulergaussriemann at gmail.com> wrote:
> 
> Hi!  I spent a good part of yesterday trying to figure this out on my own, without success, so I'm posting.  I have a 3601x2401 pixel, 300 DPI figure I export to a png using print_figure; here's sample code:
> 
> PLB.imshow(rslt) # rslt is a 3601x2401 int-valued array
> curfig = PLB.Figure
> fig = PLB.gcf() 
> # next three lines are in my code; don't seem relevant, but I include them in case they are
> ax = fig.gca()
> ax.xaxis.set_ticks([]) 
> ax.yaxis.set_ticks([])
> 
> fig.canvas.print_figure(fn + '.png',
>                         dpi=300,
>                         bbox_inches='tight', 
>                         pad_inches=-1.0 / 72)
> The resulting png is neither the right number of pixels by pixels, nor the right number of inches by incheses; what's more the resulting png differs depending on whether I use imshow or matshow (which I also tried).
> What's going on, and how do I "fix" it?  (If I really am getting the full resolution figure, despite what the png is telling me are the pixel counts, why are the physical dimensions off, and why is the png telling me that the pixel counts are different?  If it has something to do with compression, is there some  undocumented way to say "no compression" and will that have the desired effect?)
> Thanks!
> DLG 
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users

--
Jody Klymak    
http://web.uvic.ca/~jklymak/





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171020/998a88fe/attachment.html>


More information about the Matplotlib-users mailing list