I have a 2D array from which I want to produce a contour plot using matplotlib. Everything works fine saving as PNG (or other raster formats), however for including the figure in a paper I need to save to postscript format.
The problem is, the file I get is quite big (some MB) when I save to postscript. It looks like Matplotlib saves everything in vector format. While this makes sense for the axes and the labels, that would be degraded if rasterized, I would like to have the contour plot itself in raster format (which I know can be embedded inside a postscript).
Does anybody know how to do it? I'm using the Agg backend.
OK, in the end I found the answer to my own question. It required a difficult digging in the matplotlib mailing list, so I am linking here the relevant thread in the hope it will be helpful also for someone else, and possibly easier to find (by the way, no-one replied to the poor guy who sent the message).
I willl summarize here the idea in words. One has to use the set_rasterized
method, as sega_sai suggested. However, rather than applying the method to the whole figure, as I explained in my comment, the method has to be applied to the lines that comprise the contour plot. The trick is to first create a "container" for them all and to rasterize that, instead of rasterizing each individual line (which was something I already tried and gives bad results). This works fine. In the discussion I linked you can find the code for doing it.