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?
Asked
Active
Viewed 6,501 times
17
-
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 Imagemagick – Trond Kristiansen Apr 11 '11 at 08:34
2 Answers
20
You can force individual figure elements to be rasterized like this:
text(1,1,'foobar',rasterized=True)

Jouni K. Seppänen
- 43,139
- 5
- 71
- 100
-
2Thanks. I now add rasterized=True to the line properties for each plot and it works just fine. – geometrikal Apr 13 '11 at 01:00
-
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()`. – weronika Feb 28 '13 at 21:02
-
2@weronika for me it works if I first set `matplotlib.rcParams['savefig.dpi'] = 300` – Jim Garrison Oct 27 '14 at 05:07
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'.

Gael Varoquaux
- 2,466
- 2
- 24
- 12