0

It seems to be a well-known problem that matplotlib has issues correctly rendering figures on high-DPI displays. There appears to be a Jupyter workaround involving %matplotlib inline syntax, but this syntax does not work in a standalone python script. How can I apply this kind of workaround to a vanilla Python3 script (i.e. one not in a Jupyter notebook)?

Note that I don't actually care about displaying the figures on the system I'm generating them on - I'm just encoding them into PNG images. I would love to be able to set matplotlib to ignore my system DPI altogether, but it seems to be a deliberate design choice to couple the library's function with the properties of the physical system it's running on (and to prevent direct specification of image dimensions in pixel units). The result is that, on my particular machine, setting a dpi of 100 and a figure size of 1.5 produces an image 300px in size, not 150px as expected.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43

1 Answers1

0

if you are just saving files to disk, then savefig has a dpi argument that you can set, if your figure has size of 1.5 and you set the dpi in savefig to 100 then you will get the correct 150px.

this is also the case for plt.figure function, alternatively they get their default dpi from matplotlib.rcParams["savefig.dpi"] that you can modify.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • Neither `savefig` nor `rcParams` seem to avoid the retina scaling issues - even when I set the DPI manually there, something in the backend is still being "tricked" by MacOS's retina scaling and resulting in images with four times (2x in each dimension) the number of pixels I requested. The only way I've found to avoid this is to use the offscreen `agg` backend. Unfortunately, it seems like this is a case of two components (MacOS and matplotlib) saying "I know better than you" while in actuality being totally clueless. Can we PLEASE just bring back pixels? (/rant) – MooseBoys Nov 01 '22 at 23:59