how to type word to the legend left?
6 views (last 30 days)
Show older comments
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.
ㄑ
0 Comments
Accepted Answer
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)=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);
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
posAn=pos+[-widthAn 0 0 0]; % adjust left by that width
% 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!"
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!