Skip to content

Instantly share code, notes, and snippets.

@soypat
Last active January 19, 2022 12:17
Show Gist options
  • Save soypat/1f090f43261dbeea3f03221578788a6d to your computer and use it in GitHub Desktop.
Save soypat/1f090f43261dbeea3f03221578788a6d to your computer and use it in GitHub Desktop.
Force matlab to save .eps without rasterizing vector graphics
plot3(X,Y,Z) %Plot your awesome plot
set(gcf,'renderer','Painters') % 1) Change the renderer of the figure before you draw your figure or before you export. You can use the following command>>
% Change the renderer used by the PRINT command. By default it uses the same renderer that the figure uses, so you would have to type the following command to over-ride this:
print -depsc -tiff -r300 -painters <filename>.eps % modify <filename> to your hearts desire
% modular filename way:
print('-depsc','-tiff','-r300', '-painters',[filename,'.eps'])
%% Obtained from: https://www.mathworks.com/matlabcentral/answers/92521-why-does-matlab-not-export-eps-files-properly
@JoKalliauer
Copy link

I would recommend to use 'ContentType','vector' of https://de.mathworks.com/help/matlab/ref/exportgraphics.html

%EPS
exportgraphics(gcf,'myVectorFile.eps','BackgroundColor','none','ContentType','vector')
%PDF
exportgraphics(gcf,'myVectorFile.pdf','BackgroundColor','none','ContentType','vector')

an example:

clear
close all
warning('off','MATLAB:print:ContentTypeImageSuggested')%turn of irrelevant Warning (Vector is not slower than Raster)

gcf=figure(314159265);
sphere %plot3(X,Y,Z) %Plot your awesome plot
daspect([1 1 1])%assure that it is not distored

%% Uggly RasterOutput
disp('Rasterouptut')
print('-depsc','-tiff','-r300','UgglyRaster.eps')%uggly: Rastergraphics
print('-dpdf','UgglyRaster.pdf')%uggly: Rastergraphics

%% Vector Output1 (no border for PDF, and optional transparent background)
disp('exportgraphics')%for R2020a or newer https://de.mathworks.com/help/matlab/ref/exportgraphics.html
exportgraphics(gcf,'myVectorFile1.eps','BackgroundColor','none','ContentType','vector')
exportgraphics(gcf,'myVectorFile1.pdf','BackgroundColor','none','ContentType','vector')

%% Vector Output2
disp('-vector')%In R2022a -painters got replaced by -vector
print('-depsc','-tiff','-r300', '-painters','myVectorFile2.eps')%large filesize
print('-dpdf','-r300', '-painters','myVectorFile2.pdf')%uggly: has large white borders

%% Vector Ouptut3
disp('Painters') %https://www.mathworks.com/matlabcentral/answers/92521-why-does-matlab-not-export-eps-files-properly
set(gcf,'renderer','Painters')
print('-depsc','myVectorFile3.eps')%white background which is exeedes the eps-border, not transparent
print('-dpdf','myVectorFile3.pdf')%uggly: has large white borders

Best Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment