Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save figures to pdf as raster images in matplotlib

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?

like image 729
geometrikal Avatar asked Apr 10 '11 05:04

geometrikal


People also ask

How do I save a matplotlib figure as a PDF?

To save the file in PDF format, use savefig() method where the image name is myImagePDF. pdf, format = ”pdf”. To show the image, use the plt. show() method.

How do I save multiple figures as one PDF in Python?

Initialize a variable, filename, to make a pdf file. Create a user-defined function save_multi_image() to save multiple images in a PDF file. Call the save_multi_image() function with filename. Create a new PdfPages object.

How do I export a figure in matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

Which function can be used to export graph in matplotlib to PDF?

Save as PDF File If you want to export a graph with matplotlib, you will always call . savefig(path) .


1 Answers

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

text(1,1,'foobar',rasterized=True)
like image 89
Jouni K. Seppänen Avatar answered Sep 18 '22 19:09

Jouni K. Seppänen