简体   繁体   English

Agg和开罗之间的Matplotlib后端差异

[英]Matplotlib Backend Differences between Agg and Cairo

Hej, HEJ,

I'd like to produce high quality PDFs from matplotlib plots. 我想从matplotlib图中生成高质量的PDF。 Using other code, I have produced a large array of numbers, which I plot in a figure using plt.imshow. 使用其他代码,我生成了一大堆数字,我使用plt.imshow绘制在图中。 If I now produce a PDF using plt.savefig, I notice strong differences depending on which backend I use. 如果我现在使用plt.savefig生成PDF,我会发现很大的差异取决于我使用的后端。 Most importantly, the produced files get huge with the Agg or MacOSX backend, while they are reasonably small with Cairo (see examples below). 最重要的是,使用Agg或MacOSX后端生成的文件变得非常庞大,而开罗的文件相当小(参见下面的示例)。 On the other hand, the Cairo backend produces weird text in conjunction with the TeX rendering of labels. 另一方面,Cairo后端与标签的TeX渲染一起产生奇怪的文本。 This looks awful in the TeX document. 这在TeX文档中看起来很糟糕。 My question is therefore twofold: 因此我的问题是双重的:

  1. Is it possible to produce small PDF (ie presumably without interpolating the raster image to a higher resolution) using the Agg backend? 是否有可能使用Agg后端生成小PDF(即,可能没有将光栅图像插值到更高的分辨率)?
  2. 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) 可以更改Cairo后端的一些文本设置,使其看起来与普通TeX类似(Agg后端的情况)

Here is some example code for test purposes: 以下是一些用于测试目的的示例代码:

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' )

produces a PDF of 15Kb with a bad looking xlabel. 产生15Kb的PDF,看起来很糟糕的xlabel。

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' )

produces a PDF of 986Kb which looks good. 生成986Kb的PDF,看起来不错。

I should probably add that I use matplotlib 1.0.1 with python 2.6.7 on OSX 10.6.8. 我应该补充一点,我在OSX 10.6.8上使用matplotlib 1.0.1和python 2.6.7。 In the comments, someone requested the output of grep -a Font agg.pdf : 在评论中,有人要求输出grep -a Font agg.pdf

/Shading 6 0 R /Font 3 0 R >>
<< /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 >>
<< /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 >>
%!PS-AdobeFont-1.0: CMR12 003.002
%Copyright:  (<http://www.ams.org>), 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<http://www.ams.org>\051, with Reserved Font Name CMR12.) readonly def
<< /BaseFont /CMR12 /Type /Font /Subtype /Type1 /FontDescriptor 19 0 R

As suggested by steabert in the comments above, a workaround is exporting the graphics in a different format and then convert it to PDF afterwards. 正如steabert在上面的评论中所建议的,解决方法是以不同的格式导出图形,然后将其转换为PDF。 Adjusting my example from above, the workflow could look something like this: 从上面调整我的示例,工作流可能看起来像这样:

import os
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.eps')

os.system('epspdf agg.eps agg.pdf')

producing a file of 16 Kb which looks good. 生成一个16 Kb的文件看起来不错。 There is still one difference to the examples presented above: Using the (E)PS pipeline seems to ignore the interpolation='nearest' option, ie the image appears blurry in the final PDF. 上面给出的示例仍然存在一个差异:使用(E)PS管道似乎忽略插值='最近'选项,即图像在最终PDF中显得模糊。 Luckily, I can live with that, but it might be interesting to look into this issue. 幸运的是,我可以忍受这一点,但调查这个问题可能会很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM