how to type word to the legend left?

6 views (last 30 days)
Yo
Yo on 27 Aug 2023
Moved: dpb on 28 Aug 2023
as the picture,
how I type word (or title?) to the legend left?
Any help on this matter would be greatly appreciated.
I would be thankful to anyone who can provide some assistance.

Accepted Answer

dpb
dpb on 27 Aug 2023
Edited: dpb on 27 Aug 2023
hL=plot(randn(10,2));
hLg=legend('Std','Avg');
%pos=hLg.Position;
pos = 1×4
0.7535 0.8210 0.1321 0.0797
%pos(1)=pos(1)-0.1;
%hA=annotation('textbox',pos,'String','NCD','FitBoxToText','off','vertical','middle');
Gotta' run, sorry...general idea, fix up the width and horizontal position so the text box doesn't overlay the legend; issue is the 'FitToText' property when on adjusts the height to that of the text in the box, not matching the legend height.
See the annotation for the text box for the details on the properties; the place where having the units in normalized figure coordinates actually works because that's how the legend is positioned.
OK....back. Let's see if we can clean this up...
pos=hLg.Position; % start with the legend location
hA=annotation('textbox','String','NCD','FitBoxToText','on', ...
'horizontal','right','vertical','middle', ...
'LineStyle','none','Margin',0,'Visible',0);
0.7535 0.8210 0.1321 0.0797
posAn=hA.Position; % the position vector; we're interested in width
delete(hA) % ok, we know how wide it thinks has to be, done with temp
0.3000 0.3000 0.1000 0.1000
widthAn = 0.1000
posAn=pos+[-widthAn 0 0 0]; % adjust left by that width
posAn = 1×4
0.6535 0.8210 0.1321 0.0797
% now try for real with the text
hA=annotation('textbox',posAn,'String','NCD','FitBoxToText','off', ...
'horizontal','left','vertical','middle', ...
'LineStyle','none','Visible',1);
pos(1)=posAn(1); pos(3)=pos(3)+widthAn; % then adust the legend
hLg.Position=pos;
Well, that's the right idea; just not yet quite right on manipulating the two objects' width and left positions to get the text placed precisely where want within the outline, but it can be done...with a lot of tedium, first, of course.
I gotta' run again...good luck. "Salt to suit!"
  1 Comment
Yo
Yo on 28 Aug 2023
Moved: dpb on 28 Aug 2023
Thanks a lot!!!
I final use your code and adjusrt the property inspector manually (legend edgecolor set white color)
then it can work ,
I really appreciate your help on this. Thank you so much for assisting me!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!