Adjusting the legend spacing

78 views (last 30 days)
I have slightly lengthy legend titles as shown in the figure below. Is there a way to adjust the location of 'a val' / 'b val' more towards the centre of their title name (move leftwards)?
Similarly for 'c limit' /'d limit' ( move towards right!!)
The sample code used to generate the plot is given below
a=rand(1,10);
b=rand(1,10);
c=ones(1,10)*0.7;
d=ones(1,10)*0.2;
figure;
p1=plot(a,'o-r');
hold on
p2=plot(b,'s-b');
p3=plot(c,'-.k');
p4=plot(d,'-.k');
lg=legend([p1 p2 p3 p4],'a val','b val','c limit','d limit');
lg.NumColumns=2;
title(lg,'raw data values from experiments limitation values');
ylim([0 1.2])

Accepted Answer

Image Analyst
Image Analyst on 25 Nov 2021
I don't know of any way in the legend() function. You might try creating strings for your legends manually and put a \n at the end of the string or beginning of the string.
Alternatively make your own legend manually with the text() function and place them wherever you want.

More Answers (1)

the cyclist
the cyclist on 25 Nov 2021
Not directly answering your question, but a different (and more conventional) approach would be to move the limit annotations out of the legend.
rng default
a=rand(1,10);
b=rand(1,10);
figure
hold on
p1=plot(a,'o-r');
p2=plot(b,'s-b');
yline(0.7,'-.')
yline(0.2,'-.')
text(9,0.675,'d limit')
text(9,0.175,'c limit')
lg=legend([p1 p2],{'a','b'});
title('raw data values from experiments');
ylim([0 1.2])
  1 Comment
Shivaprasad Shridhara Bhat
Thanks alot @the cyclist for you effort. I am aware of this method.
I am particularly looking for ways to adjust the legend locations.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!