Adjusting the legend spacing

82 次查看(过去 30 天)
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])

采纳的回答

Image Analyst
Image Analyst 2021-11-25
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.

更多回答(1 个)

the cyclist
the cyclist 2021-11-25
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 个评论
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by