title: Matplotlib Backend Differences between Agg and Cairo
body: <p>Hej,</p>
  
  <p>I'd like to produce high quality PDFs from matplotlib plots. Using other code, I have produced a large array of numbers, which I plot in a figure using plt.imshow. If I now produce a PDF using plt.savefig, I notice strong differences depending on which backend I use. Most importantly, the produced files get huge with the Agg or MacOSX backend, while they are reasonably small with Cairo (see examples below). On the other hand, the Cairo backend produces weird text in conjunction with the TeX rendering of labels. This looks awful in the TeX document. My question is therefore twofold:</p>
  
  <ol>
  <li>Is it possible to produce small PDF (i.e. presumably without interpolating the raster image to a higher resolution) using the Agg backend?</li>
  <li>Can one change some text settings for the Cairo backend such that it looks similar to ordinary TeX (which is the case for the Agg backend)</li>
  </ol>
  
  <p>Here is some example code for test purposes:</p>
  
  <pre><code>import matplotlib as mpl
  mpl.use( "cairo" )
  
  import numpy as np
  import matplotlib.pyplot as plt
  plt.rcParams['text.usetex'] = True
  
  data = np.random.rand( 50, 50 )
  
  plt.imshow( data, interpolation='nearest' )
  plt.xlabel( 'X Label' )
  plt.savefig( 'cairo.pdf' )
  </code></pre>
  
  <p>produces a PDF of 15Kb with a bad looking xlabel.</p>
  
  <pre><code>import matplotlib as mpl
  mpl.use( "agg" )
  
  import numpy as np
  import matplotlib.pyplot as plt
  plt.rcParams['text.usetex'] = True
  
  data = np.random.rand( 50, 50 )
  
  plt.imshow( data, interpolation='nearest' )
  plt.xlabel( 'X Label' )
  plt.savefig( 'agg.pdf' )
  </code></pre>
  
  <p>produces a PDF of 986Kb which looks good.</p>
  
  <p>I should probably add that I use matplotlib 1.0.1 with python 2.6.7 on OSX 10.6.8. In the comments, someone requested the output of <code>grep -a Font agg.pdf</code>:</p>
  
  <pre><code>/Shading 6 0 R /Font 3 0 R &gt;&gt;
  &lt;&lt; /FontFile 16 0 R /Descent -285 /FontBBox [ -174 -285 1001 953 ]
  /StemV 50 /Flags 4 /XHeight 500 /Type /FontDescriptor
  /FontName /NimbusSanL-Regu /CapHeight 1000 /FontFamily (Nimbus Sans L)
  %!PS-AdobeFont-1.0: NimbusSanL-Regu 1.05a
  FontDirectory/NimbusSanL-Regu known{/NimbusSanL-Regu findfont dup/UniqueID known{dup
  /UniqueID get 5020902 eq exch/FontType get 1 eq and}{pop false}ifelse
  /FontType 1 def
  /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def
  /FontName /NimbusSanL-Regu def
  /FontBBox [-174 -285 1001 953 ]readonly def
  /FontInfo 9 dict dup begin
  /BaseFont /NimbusSanL-Regu /Type /Font /Subtype /Type1
  /FontDescriptor 15 0 R /Widths 13 0 R /LastChar 255 /FirstChar 0 &gt;&gt;
  &lt;&lt; /FontFile 20 0 R /Descent -251 /FontBBox [ -34 -251 988 750 ] /StemV 50
  /Flags 4 /XHeight 500 /Type /FontDescriptor /FontName /CMR12
  /CapHeight 1000 /FontFamily (Computer Modern) /ItalicAngle 0 /Ascent 750 &gt;&gt;
  %!PS-AdobeFont-1.0: CMR12 003.002
  %Copyright:  (&lt;http://www.ams.org&gt;), with Reserved Font Name CMR12.
  % This Font Software is licensed under the SIL Open Font License, Version 1.1.
  FontDirectory/CMR12 known{/CMR12 findfont dup/UniqueID known{dup
  /UniqueID get 5000794 eq exch/FontType get 1 eq and}{pop false}ifelse
  /FontType 1 def
  /FontMatrix [0.001 0 0 0.001 0 0 ]readonly def
  /FontName /CMR12 def
  /FontBBox {-34 -251 988 750 }readonly def
  /FontInfo 9 dict dup begin
   /Notice (Copyright \050c\051 1997, 2009 American Mathematical Society \050&lt;http://www.ams.org&gt;\051, with Reserved Font Name CMR12.) readonly def
  &lt;&lt; /BaseFont /CMR12 /Type /Font /Subtype /Type1 /FontDescriptor 19 0 R
  </code></pre>