exportgraphics: adding a margin
Mostrar comentarios más antiguos
Is it possible to add a margin to figures saved using the exportgraphics function?
It crops a tad too tightly for my needs.
Respuesta aceptada
Más respuestas (4)
Paul Wintz
el 15 de Sept. de 2021
Editada: Paul Wintz
el 18 de Nov. de 2021
You can force exportgraphics to export the entire figure by drawing an white rectange around it with the command
annotation('rectangle',[0 0 1 1],'Color','w');
To adjust the margins, change the dimensions [0 0 1 1].
As noted by @David Walwark in the comments, the plot will not be interactive while it is annotated. To restore interactivity, delete the annotation after calling exportgraphics.
a = annotation('rectangle',[0 0 1 1],'Color','w');
% -> Save figure here <-
delete(a)
4 comentarios
Fatemeh Sadeghihassanabadi
el 11 de Oct. de 2021
David Walwark
el 17 de Nov. de 2021
Excellent, this really helped. If one wants the figure to remain interactive then it is best to delete the handle to that annotation after exportgraphics is called.
Bill Tubbs
el 23 de Mzo. de 2023
Editada: Bill Tubbs
el 23 de Mzo. de 2023
This didn't work for me. Presumably when doing 2 subplots you need to apply this annotation to each subplot?

I tried adjusting the margins to [-0.05 -0.05 1.05 1.05] and it raises:
Error using annotation (line 116)
Position values must be between 0 and 1.
Alexander Moody
el 28 de Ag. de 2023
Had the same issue... this worked for me, but I had to change the color of the annotation rectangle to something other than white for it to work.
Kiran Felix Robert
el 24 de Jun. de 2021
1 voto
Hi Dormant,
If you are trying to export multiple sub-images/plots onto a single figure, you can explore the tiledlayout to introduce spacing using the 'TileSpacing' Name-Value pair.
2 comentarios
Paul Wintz
el 15 de Sept. de 2021
This doesn't answer the question. How do we adjust the margins on the outside of a figure saved with exportgraphics?
Bill Tubbs
el 23 de Mzo. de 2023
This worked for me. When I did my 2-plot figure using tiledlayout instead of subplots the outer margin was significantly wider.
dormant
el 8 de Oct. de 2024
0 votos
Andreas Sprenger
el 28 de En. de 2025
0 votos
I use the plotedit function to refrain cropping of plots (particularly with multiple subplots.
hfig = figure('WindowState', 'maximized');
plotedit(hfig);
% subsequent plotting commands
You get a tiny blue line at the outer border of the page but the cropping is gone :-)
Categorías
Más información sobre Printing and Saving en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!