介绍
有时候,我们在 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()