Legend with multiple columns and problem with alignement for last entry

Dear matlab comunity,
I used the legend property "NumColumns=2" to get 2 colums in my legend. However, I would like the last (longer) entry NOT to affect the alignment of the upper ones ("c" and "d"). Is there a possibility to do this?

Answers (1)

There does not appear to be anying in Legend Properties that would directly address that problem. The only approach I can think of would be to break up ‘Long sentence’ into more than one line. This reduces the gap, however it would not completely eliminate it.
You can do that easily enough with the sprintf function —
x = 0:0.01:1;
y = sin((1:5).'*2*pi*x);
figure
plot(x, y(1,:), 'DisplayName','a')
hold on
plot(x, y(2,:), 'DisplayName','b')
plot(x, y(5,:), 'DisplayName',sprintf('%s\n%s','Long','sentence'))
plot(x, y(3,:), 'DisplayName','c')
plot(x, y(4,:), 'DisplayName','d')
hold off
legend('Location','best', 'NumColumns',2)
.

Products

Release

R2023b

Asked:

on 6 Feb 2024

Answered:

on 6 Feb 2024

Community Treasure Hunt

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

Start Hunting!