Question

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.

Was it helpful?

Solution

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top