How to add an independent text in MATLAB plot legend
Asked Answered
P

4

6

I need an additional text in the legend that is not related with graphical data together with the legend captions. Something like this (it was made in OriginLab):

enter image description here Following to this link Add custom legend without any relation to the graph I can add some text using plot(NaN,NaN,'display','add info here2', 'linestyle', 'none'). But there is an indent in this text:

enter image description here

How to avoid this indent? And is there a more elegant method to add the text that is not associated with the legend together with the legend captions?

Pseudonymous answered 23/1, 2017 at 10:41 Comment(1)
Not saying that it's the best solution, but perhaps you could find it useful somehow: legtools.Trailer
S
8

The legend function will return as its second output argument handles for all of the components that make up the symbols and text in the legend. You can therefore plot "dummy" lines as placeholders in the legend, reorder the handles when creating the legend to put the text where you want it, and modify the legend objects accordingly. Here's an example:

x = linspace(0, 2*pi, 100);
hl = plot(x, [sin(x); cos(x); tan(x); nan(size(x))].');          % Add a line of NaNs
axis([0 2*pi -4 4]);
[~, objH] = legend(hl([1 2 4 3]), 'sin', 'cos', 'junk', 'tan');  % Reorder handles
set(findobj(objH, 'Tag', 'junk'), 'Vis', 'off');           % Make "junk" lines invisible
pos = get(objH(3), 'Pos');                                 % Get text box position
set(objH(3), 'Pos', [0.1 pos(2:3)], 'String', 'also...');  % Stretch box and change text

enter image description here

Squeak answered 23/1, 2017 at 20:34 Comment(1)
Thank you for the answer. It looks like that I need.Pseudonymous
T
2

You can use annotations. It's not perfect, but with few adjustments you'll get what you want. Here's an example:

% somthing to plot:
x = [0:0.1:5; 5:0.1:10].';
y = sin(x);
% plot the real data:
plot(x,y);
hold on
% make some space in the legend:
Spacing_lines = 3;
h = plot(nan(size(x,1),Spacing_lines));
hold off
set(h,{'Color'},{'w'}); % clear the dummy lines
% place the legend:
hl = legend([{'lable1','lable2'} repmat({''},1,Spacing_lines)]);
% add your text:
annotation('textbox',hl.Position,'String',{'Some info';'in 2 lines'},...
    'VerticalAlignment','Bottom','Edgecolor','none');

And from this you get:

txt 2 legend

Tiberias answered 23/1, 2017 at 20:3 Comment(2)
Thanks for the answer. Unfortunately, the solution of "gnovice" is better.Pseudonymous
@AlexanderKorovin I agree :)Tiberias
P
1

You can just add any text to any point of plot in this way:

txt1 = 'some information';
text(x1,y1,txt1)

where x1, y1 - coordinates.

enter image description here

By the way function text function has a lot of different properties (colors, font size, alignment etc.).

Puca answered 23/1, 2017 at 10:50 Comment(1)
Thanks for the answer. But this is not a good solution. In this situation, I have to adjust the text with the legend. In addition, there is a problem if I want to place the text between the descriptions of the two curves, as shown in my question. Of course I can create a legend by myself (but I must know colors, linestyles and symbols of my curves. So this is a hard work).Pseudonymous
O
0

I think the easiest way is to just create some dummy function, plot it but set the color="none" - that way it will only show up in the legend (if that is where you wanted it).

Odont answered 11/2, 2017 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.