3

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 attached here

Note: The data shown below are completely representative. I am not keen on finding ways to replot this dataset differently to illustrate the same idea. The aim is to adjust the legend location.


% random variables 
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])

sample figure

3
  • Which version of MATLAB are you using?
    – Wolfie
    Commented Nov 26, 2021 at 8:49
  • @Wolfie I am currently using Matlab versions 2020b
    – Shiva
    Commented Nov 26, 2021 at 9:45
  • Perhaps you can add a second legend box Commented Nov 27, 2021 at 16:41

1 Answer 1

1

This is some kind of ugly last-resort solution, but what worked for me was switching the interpreter to latex and then using latex commands to add additional space.

lg=legend([p1 p2 p3 p4], ['a val \hspace{50pt}'  ],'b val','c limit','d limit');
lg.Interpreter = 'latex';

Please note, that this changes also the font.

Not the answer you're looking for? Browse other questions tagged or ask your own question.