[Matplotlib] 减小生成的 PDF 文件大小 - Rasterized

介绍

有时候,我们在 Matplotlib 中画图的时候,如果元素过多,例如图中有几十万个点,那么图片的大小会非常大。本文介绍如何减小生成的 PDF 文件大小。

解决方案

使用rasterized=True参数。

测试代码

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(0, 1, int(1e6))
y = np.random.normal(0, 1, int(1e6))
radius = np.random.normal(0, 1, int(1e6))

plt.scatter(x, y, s=radius, c=radius, cmap='Oranges', rasterized=True)

plt.show()

结果

fig

原始 PDF 文件:45.1 MB

Rasterized PDF 文件:212 KB

发布了40 篇原创文章 · 获赞 84 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xovee/article/details/104993678