Reducing the size of pdf figure file in matplotlib

笑着哭i 提交于 2020-06-07 21:36:12

问题


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)

the figure size are quite big. However, on saving them as png file:

fig.savefig('filename.png',dpi=300)

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?


回答1:


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)

Here, x, y are some plot coordinates. You basically have to use the additionally keyword argument raterized to achieve the effect.




回答2:


I think using "rasterized = True" effectively saves the image similarly to png format. When you zoom in, you will see blurring pixels.

If you want the figures to be high quality, my suggestion is to sample from the data and make a plot. The pdf file size is roughly the amount of data points it need to remember.



来源:https://stackoverflow.com/questions/10685495/reducing-the-size-of-pdf-figure-file-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!