python - Reducing size of vectorized contourplot -
i include filled contour plot pdf document (for example tex document). using pyplots contourf, , saving pdf pyplots savefig. problem size of plots becomes rather big compared high resolution png.
one way reduce size of course reduce number of levels in plot, few levels gives poor plot. i'm searching simple way example let colors of plot saved png, axes, ticks etc. saved vectorized.
you can using axes option set_rasterization_zorder.
anything zorder less set saved rasterized graphics, when saving vector format pdf.
for example:
import matplotlib.pyplot plt import numpy np data = np.random.rand(500,500) # fig1 save contourf vector fig1,ax1 = plt.subplots(1) ax1.contourf(data) fig1.savefig('vector.pdf') # fig2 save contourf raster fig2,ax2 = plt.subplots(1) ax2.contourf(data,zorder=-20) ax2.set_rasterization_zorder(-10) fig2.savefig('raster.pdf') # show difference in file size. "os.stat().st_size" gives file size in bytes. print os.stat('vector.pdf').st_size # 15998481 print os.stat('raster.pdf').st_size # 1186334 you can see this matplotlib example more background info.
as pointed out @tcaswell, rasterise 1 artist without having affect zorder, can use .set_rasterized. however, doesn't appear option contourf, need loop on pathcollections created contourf , set_rasterized on each of them. this:
contours = ax.contourf(data) pathcoll in contours.collections: pathcoll.set_rasterized(true)
Comments
Post a Comment