Legend not exported when position is set to "best"

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?
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
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.
I also experience this problem with saving figures as pdf's. Has there been a fix?

Sign in to comment.

Answers (0)

Products

Release

R2024b

Asked:

on 21 Feb 2025

Commented:

on 21 Mar 2026

Community Treasure Hunt

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

Start Hunting!