您可以使用
Axes选项执行此操作
set_rasterization_zorder。
任何
zorder小于您设置的值的内容都将保存为栅格化的图形,即使保存为矢量格式(例如)也是如此
例如:
import matplotlib.pyplot as pltimport numpy as npdata = np.random.rand(500,500)# fig1 will save the contourf as a vectorfig1,ax1 = plt.subplots(1)ax1.contourf(data)fig1.savefig('vector.pdf')# fig2 will save the contourf as a rasterfig2,ax2 = plt.subplots(1)ax2.contourf(data,zorder=-20)ax2.set_rasterization_zorder(-10)fig2.savefig('raster.pdf')# Show the difference in file size. "os.stat().st_size" gives the file size in bytes.print os.stat('vector.pdf').st_size# 15998481print os.stat('raster.pdf').st_size# 1186334您可以看到此matplotlib示例以获取更多背景信息。
正如@tcaswell指出的那样,要光栅化一位艺术家而不必影响它
zorder,可以使用
.set_rasterized。但是,这似乎不是的选项
contourf,因此您需要遍历每个对象上和上
PathCollections创建的对象。像这样:
contourf``set_rasterized
contours = ax.contourf(data)for pathcoll in contours.collections: pathcoll.set_rasterized(True)



