How to save figures to pdf as raster images in matplotlib
Asked Answered
E

2

17

I have some complex graphs made using matplotlib. Saving them to a pdf using the savefig command uses a vector format, and the pdf takes ages to open. Is there any way to save the figure to pdf as a raster image to get around this problem?

Eyewash answered 10/4, 2011 at 5:27 Comment(1)
If you want to save to different format within matplotlib just use savefig("filename.png") to save to png raster. If you want to continue saving to PDF and then convert I agree with Gael to use ImagemagickBuehler
I
20

You can force individual figure elements to be rasterized like this:

text(1,1,'foobar',rasterized=True)
Immunology answered 12/4, 2011 at 16:32 Comment(3)
Thanks. I now add rasterized=True to the line properties for each plot and it works just fine.Eyewash
Awesome! Now if only I could control the dpi of the rasterizing... It seems to ignore the dpi argument I passed to figure() and savefig().Dialogism
@Dialogism for me it works if I first set matplotlib.rcParams['savefig.dpi'] = 300Galliwasp
M
3

Not that I know, but you can use the 'convert' program (ImageMagick') to convert a jpg to a pdf: `convert file.jpg file.pdf'.

Methodology answered 10/4, 2011 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.