Exporting figures with minimal whitespace via exportgraphics() works only for .png, but not for .eps or .pdf format?

36 vues (au cours des 30 derniers jours)
I'm trying to create some plots and output them out as .eps or .pdf files, with the whitespace properly detected and removed. As I'm running a new enough version of MATLAB (R2021b), I have used the guide Save Plot with Minimal White Space. However, this only works when exporting as .png, otherwise there is plenty of whitespace left. I've also provided a simple example code, which, for me, outputs three files, and only the .png file is actually cropped to remove the whitespace.
My question is if I'm doing something wrong, and is there any way to get the same effect for .eps or .pdf files?
z = peaks(25);
figure
mesh(z)
set(gca, 'Visible', 'off')
% Png file
exportgraphics(gcf,'AnnotatedPlot.png')
% Pdf file
exportgraphics(gcf,'AnnotatedPlot.pdf')
% Eps file
exportgraphics(gcf,'AnnotatedPlot.eps')

Réponses (1)

Sandeep Mishra
Sandeep Mishra le 6 Sep 2024
Hi,
It appears that you are encountering an issue with the 'exportgraphics' function, where exporting a 'figure' to PDF and EPS file format results in unwanted white spaces.
To address this problem, you can modify the 'ContentType' property of the 'exportgraphics' function to 'vector'. This adjustment helps minimize the white spaces in the exported figures.
Below is the code snippet illustrating how to implement this approach:
% Export as PDF
exportgraphics(gcf,'AnnotatedPlot.pdf','ContentType','vector')
% Export as EPS
exportgraphics(gcf,'AnnotatedPlot.eps','ContentType','vector')
Please refer to the below documentation to learn more about ‘ContentType’ property of ‘exportgraphics’ function in MATLAB: https://www.mathworks.com/help/releases/R2021b/matlab/ref/exportgraphics.html#mw_5bdc680e-deea-429e-b58e-24e3de1083f7
I hope this helps.

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by