Setting dpi in savefig has unexpected results for PDFs

I recently noticed that setting the dpi for savefig doesn’t work as expected when saving to pdf. Take the following code, for example:

import matplotlib.pyplot as plt

plt.figure(figsize=(8,6))

plt.plot([1,2])

plt.savefig(‘test.png’, dpi=100)

plt.savefig(‘test.pdf’, dpi=100)

The resulting png file is 800 x 600 (as expected), while the pdf file is 576 x 432 [which is (800 x 600) * 72/100]. I found an old thread suggesting that a dpi of 72 should be hard coded into the PDF renderer for compatibility with older versions of the PDF spec. That makes sense; however, it’d be nice if the docstring for savefig told users about his behavior.

Below, is a patch to the savefig docstring. I’m sure someone else could word this better, but I thought I’d at least try.

Best,

-Tony

P.S. maybe enough time has passed that most people have adopted PDF viewers/parsers using PDF >= 1.6, and this hard-coded dpi could be removed? Just a thought.

Index: lib/matplotlib/figure.py

···

===================================================================

— lib/matplotlib/figure.py (revision 8561)

+++ lib/matplotlib/figure.py (working copy)

@@ -1018,7 +1018,10 @@

dpi: [ None | scalar > 0 ]

The resolution in dots per inch. If None it will default to

  •        the value ``savefig.dpi`` in the matplotlibrc file.
    
  •        the value ``savefig.dpi`` in the matplotlibrc file. NOTE: when
    
  •        saving to pdf, the dpi will not affect the page dimensions (which
    
  •        is always 72 dpi), but it will affect the resolution of rasterized
    
  •        elements in the plot.
    

facecolor, edgecolor:

the colors of the figure rectangle

I recently noticed that setting the dpi for savefig doesn't work as
expected when saving to pdf. Take the following code, for example:

>>> import matplotlib.pyplot as plt
>>>
>>> plt.figure(figsize=(8,6))
>>> plt.plot([1,2])
>>> plt.savefig('test.png', dpi=100)
>>> plt.savefig('test.pdf', dpi=100)

The resulting png file is 800 x 600 (as expected), while the pdf file is
576 x 432 [which is (800 x 600) * 72/100]. I found an old thread

No, 576 x 432 is the paper size in points, not dots, so it is 8x6 inches as requested. Since the content is all vector-based, there is no notion of dots or pixels in test.pdf.

<http://old.nabble.com/figsize-anomaly-in-pdf-td15234278.html&gt;
suggesting that a dpi of 72 should be hard coded into the PDF renderer
for compatibility with older versions of the PDF spec. That makes sense;
however, it'd be nice if the docstring for savefig told users about his
behavior.

Yes, the docstring probably should point out that for vector backends (pdf, ps, svg), the dpi setting affects only the resolution of rasterized components.

Below, is a patch to the savefig docstring. I'm sure someone else could
word this better, but I thought I'd at least try.

Best,
-Tony

P.S. maybe enough time has passed that most people have adopted PDF
viewers/parsers using PDF >= 1.6, and this hard-coded dpi could be
removed? Just a thought.

No; I think the present behavior is correct, regardless of the pdf version. It is not really a hard-coded dpi at all, it is a confusing aspect of the way mpl uses variables called "dpi". (But someone who knows more about pdf may correct me if necessary.)

Eric

···

On 07/16/2010 09:45 AM, Tony S Yu wrote:

Index: lib/matplotlib/figure.py

--- lib/matplotlib/figure.py (revision 8561)
+++ lib/matplotlib/figure.py (working copy)
@@ -1018,7 +1018,10 @@
*dpi*: [ None | scalar > 0 ]
The resolution in dots per inch. If *None* it will default to
- the value ``savefig.dpi`` in the matplotlibrc file.
+ the value ``savefig.dpi`` in the matplotlibrc file. NOTE: when
+ saving to pdf, the dpi will not affect the page dimensions (which
+ is always 72 dpi), but it will affect the resolution of rasterized
+ elements in the plot.
*facecolor*, *edgecolor*:
the colors of the figure rectangle

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first

_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
matplotlib-devel List Signup and Options

I recently noticed that setting the dpi for savefig doesn’t work as
expected when saving to pdf. Take the following code, for example:

import matplotlib.pyplot as plt

plt.figure(figsize=(8,6))
plt.plot([1,2])
plt.savefig(‘test.png’, dpi=100)
plt.savefig(‘test.pdf’, dpi=100)

The resulting png file is 800 x 600 (as expected), while the pdf file is
576 x 432 [which is (800 x 600) * 72/100]. I found an old thread

No, 576 x 432 is the paper size in points, not dots, so it is 8x6 inches
as requested. Since the content is all vector-based, there is no notion
of dots or pixels in test.pdf.

[snip]

P.S. maybe enough time has passed that most people have adopted PDF
viewers/parsers using PDF >= 1.6, and this hard-coded dpi could be
removed? Just a thought.

No; I think the present behavior is correct, regardless of the pdf
version. It is not really a hard-coded dpi at all, it is a confusing
aspect of the way mpl uses variables called “dpi”. (But someone who
knows more about pdf may correct me if necessary.)

Eric

Hmm, that makes sense.

I was just confused by the fact that some programs use points as dots or pixels when displaying to screen. For example, that’s how my presentation software chooses to display embedded PDFs. As a result, an 8in x 6in PNG shows up as a different size than an 8in x 6in PDF. (Yes, you can resize, but this screws up all the font sizes). Anyway, I guess this is more an issue with my presentation software than anything else.

Thanks for the clarification, Eric.

-Tony

···

On Jul 16, 2010, at 4:08 PM, Eric Firing wrote:

On 07/16/2010 09:45 AM, Tony S Yu wrote: