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

David Goldsmith eulergaussriemann at gmail.com
Fri Oct 20 18:36:30 EDT 2017


Thanks.  Does it have pylab’s range of built in color maps?  Does it even
use colormaps, or do I have to write my own (or “copy” them from MPL).

DLG

On Fri, Oct 20, 2017 at 3:28 PM Warren Weckesser <warren.weckesser at gmail.com>
wrote:

> On Fri, Oct 20, 2017 at 5: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:
>>
>>
>
> David,
>
> If you are trying to write the numpy array directly to a PNG file, an
> alternative to matplotlib is a package that I created called numpngw:
> https://pypi.python.org/pypi/numpngw (github:
> https://github.com/WarrenWeckesser/numpngw).  Both those links show
> several examples of its use.   numpngw uses just Python and NumPy, so it is
> easy to install.
>
> Warren
>
>
> 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
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171020/256c5a63/attachment-0001.html>


More information about the Matplotlib-users mailing list