[Matplotlib-users] Exporting plot with given scale and DPI

Eric Firing efiring at hawaii.edu
Mon Jan 8 14:29:44 EST 2018

You are correct that if you are displaying a plot on the screen, the 
figure size will be changed to fit.  This is a shortcoming of all the 
mpl backends; the plot windows should have automatic scrollbars instead. 
The workaround is simple, though: When you make the figure, use a small 
enough dpi so that the window will fit on the screen.  Then use your 
desired dpi when you save:

fig, ax = plt.subplots(figsize=(20,30), dpi=20)
# ... plot something...
fig.savefig('bigplot.png', dpi=150)

Assuming you have a typical screen size, the above will result in the 
following output from the "file" command (in Linux or OSX):

bigplot.png: PNG image data, 3000 x 4500, 8-bit/color RGBA, non-interlaced

As you see, it is consistent with the requested figsize and file dpi.


On 2018/01/08 7:45 AM, Alexander Bruy wrote:
> It is not clear from the docs if this function preserves dimensions and DPI
> when exporting plot into PNG.
> Also as I understand it resizes plot on screen and does not play well with
> large sizes.
> 2018-01-08 19:20 GMT+02:00 Jody Klymak <jklymak at uvic.ca>:
>> Does `fig.set_size_inches()` do what you want?
>> Cheers,   Jody
>>> On 8 Jan 2018, at 08:44, Alexander Bruy <alexander.bruy at gmail.com> wrote:
>>> Hi all.
>>> I use matplotlib inside PyQt app and want to export plot into PNG image with
>>> some user-defined scale and DPI. So after printing that image I should be able
>>> to measure on the paper according to the scale.
>>> For example, length of the profile line is 500m (this is my x-axis),
>>> scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in
>>> a 5cm print out. Taking 150 DPI into account resulting bitmap should
>>> be 295 pixels wide:
>>> 1cm is 0.393701 inches
>>> 5cm is 1.968505 inches
>>> 1.968505inches * 150DPI = 295.27575 px
>>> Also it is necessary to take into account that resulting image may be
>>> really big, for
>>> example if profile line length is several kilometers and DPI is 600.
>>> As I can see there is a 'dpi' parameter in the savefig() call, but I
>>> can't figure out how
>>> to adjust size of the existing figure when exporting. Can you help me?
>>> Thanks
>>> --
>>> Alexander Bruy
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users at python.org
>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>> --
>> Jody Klymak
>> http://web.uvic.ca/~jklymak/

More information about the Matplotlib-users mailing list