Rasterization of matplotlib axis contents (but not frames, labels)

In this article, I create graphs of deformed finite element meshes, which I visualize using the matplotlib polycollection. Images are saved as pdf.

Problems arise for high-density networks, for which a naive approach leads to files that are too large and make them too intense to be practical.

For these grids, it is really pointless to build each element as a polygon; it can be easily rasterized, as is done when saving the image in jpg or png format. However, for printing, I would like to dwell on a clear frame, inscriptions and annotations.

Does anyone know if it is possible to achieve such hybrid rasterization in matplotlib?

I can think of solutions involving imshow and bypass the multi-collection, but I would prefer to use the built-in components of matplotlib.

Thank you for your advice.

+6
source share
1 answer

Just pass the rasterized=True keyword to your collection constructor. Example:

 col = collections.PolyCollection(<arguments>, rasterized=True) 

This allows only selective rasterization of this element (for example, if you made a normal plot on top of it, it will be vectorized by default). Most commands, such as plot or imshow , can also accept the rasterized keyword. If you want to rasterize the entire drawing (including labels and annotations), this would do this:

 fig = plt.figure() a = fig.add_subplot(1,1,1, rasterized=True) 

(But this is not what you want, as stated in the question.)

+9
source

All Articles