Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rasterizing matplotlib axis contents (but not frame, labels)

For an article I am generating plots of deformed finite element meshes, which I visualize using matplotlib's polycollection. The images are saved as pdf.

Problems arise for high density meshes, for which the naive approach results in files that are too large and rendering too intensive to be practical.

For these meshes it really makes no sense to plot each element as a polygon; it could easily be rasterized, as is done when saving the image as jpg or png. However, for print I would like to hold on to a sharp frame, labels, and annotations.

Does anyone know if it is possible to achieve this kind of hybrid rasterization in matplotlib?

I can think of solutions involving imshow, and bypassing polycollection, but I would much prefer to use matplotlib's built-in components.

Thanks for your advice.

like image 433
gertjan Avatar asked Dec 25 '12 17:12

gertjan


1 Answers

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

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

This allows a selective rasterization of that element only (e.g., if you did a normal plot on top of it, it would be vectorized by default). Most commands like plot or imshow can also take the rasterized keyword. If one wants to rasterize the whole figure (including labels and annotations), this would do it:

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

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

like image 94
tiago Avatar answered Oct 22 '22 15:10

tiago