Reducing the size of a vectorized outline

I would like to include a completed outline graph in a pdf document (e.g. TeX document). Currently I use pyplot contourfand save pdfwith pyplot savefig. The problem is that the size of the graphs becomes quite large compared to high resolution png.

One way to reduce the size is, of course, to reduce the number of levels in the plot, but too few levels give a bad plot. I am looking for a simple way, for example, to save plot colors as png, and axes, ticks, etc. Will be saved in vector.

+1
source share
1 answer

You can do this using the option Axes set_rasterization_zorder.

, zorder , , , , pdf.

:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(500,500)

# fig1 will save the contourf as a vector
fig1,ax1 = plt.subplots(1)
ax1.contourf(data)
fig1.savefig('vector.pdf')

# fig2 will save the contourf as a raster
fig2,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
# 15998481
print 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)
+6

All Articles