how to type word to the legend left?

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.

 Risposta accettata

dpb
dpb il 27 Ago 2023
Modificato: dpb il 27 Ago 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 Commento

Yo
Yo il 28 Ago 2023
Spostato: dpb il 28 Ago 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!!

Accedi per commentare.

Più risposte (0)

Richiesto:

Yo
il 27 Ago 2023

Spostato:

dpb
il 28 Ago 2023

Community Treasure Hunt

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

Start Hunting!

Translated by