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?
How to save figures to pdf as raster images in matplotlib
Asked Answered
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 –
Buehler
You can force individual figure elements to be rasterized like this:
text(1,1,'foobar',rasterized=True)
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'] = 300
–
Galliwasp Not that I know, but you can use the 'convert' program (ImageMagick') to convert a jpg to a pdf: `convert file.jpg file.pdf'.
© 2022 - 2024 — McMap. All rights reserved.