Legend not exported when position is set to "best"
Show older comments
I'm creating a figure with plot and legend and I set the legend's position to "best" like this:
legend("...", Location="best")
After exporting:
exportgraphics(f, fullfile(opts.output_path,fig_name), 'Resolution', 300);
The legend is not on the figure.
If I set the location to someting else, like "southwest", it works.
Does anyone know if there is a solution to this?
Thanks!
4 Comments
I could not reproduce the problem, although I had to guess about some things, such as the output file type.
plot(1:10)
legend("Up and away", Location = "best")
exportgraphics(gcf,"test.png",Resolution = 300)
On my MacBook, running R2025a Prerelease, the generated PNG file does contain the legend.
Can you give us a complete, runnable example? And can you tell us your MATLAB version and computer type?
Andreas Justin
on 2 Oct 2025
Edited: Andreas Justin
on 2 Oct 2025
f = figure
plot(magic(5))
l = legend('show')
l.Location = "best"
print(f, "bla.pdf", '-dpdf', '-bestfit');
% MATLAB Version: 25.1.0.2973910 (R2025a) Update 1

the cyclist
on 3 Oct 2025
I was able to reproduce this (and also in R2025b).
Other factoids:
- The behavior did not depend on the 'bestfit' parameter in the print function.
- The behavior occurs for PDF, but not for PNG (but the position of the legend changes in the PNG output)
- "Best" chooses the NorthEast location ... but choosing NorthEast directly does not lead to this behavior
I suggest writing up a Support request.
Nick
on 21 Mar 2026
I also experience this problem with saving figures as pdf's. Has there been a fix?
Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!