减少matplotlib中pdf图文件的大小 [英] Reducing the size of pdf figure file in matplotlib
问题描述
在 matplotlib
中,我正在使用 LineCollection
绘制国家/地区的颜色,给定的。当我将图形另存为pdf文件时:
In matplotlib
, I am using LineCollection
to draw and color the countries, where the boundaries of the counties are given. When I am saving the figure as a pdf file:
fig.savefig('filename.pdf',dpi=300)
这个数字很大。但是,将它们另存为png文件时:
the figure size are quite big. However, on saving them as png file:
fig.savefig('filename.png',dpi=300)
然后使用linux convert
命令将它们转换为pdf文件很小。我尝试降低dpi,但是不会更改pdf文件的大小。有没有办法将图形直接从matplotlib中保存为较小的pdf文件?
and then converting them to pdf using linux convert
command the files are small. I tried reducing the dpi, however that do not change the pdf file size. Is there a way the figures can be saved directly as smaller-pdf files from matplotlib?
推荐答案
PDF较大,因为它包含所有矢量信息。通过保存PNG,可以生成光栅化的图像。看来,在您的情况下,您可以通过直接对图进行栅格化来生成较小的PDF:
The PDF is larger, since it contains all the vector information. By saving a PNG, you produce a rasterized image. It seems that in your case, you can produce a smaller PDF by rasterizing the plot directly:
plt.plot(x, y, 'r-', rasterized=True)
此处, x,y
是一些绘图坐标。基本上,必须使用附加关键字参数 raterized
才能达到效果。
Here, x, y
are some plot coordinates. You basically have to use the additionally keyword argument raterized
to achieve the effect.
这篇关于减少matplotlib中pdf图文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!