Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an independent text in MATLAB plot legend

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?

like image 747
Alexander Korovin Avatar asked Jan 23 '17 10:01

Alexander Korovin


People also ask

How do you add text to a plot in MATLAB?

To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates. text(___, Name,Value ) specifies Text object properties using one or more name-value pairs.

How do I add a title to a legend in MATLAB?

To add a legend title, set the String property of the legend text object. To change the title appearance, such as the font style or color, set legend text properties. For a list, see Text Properties. plot(rand(3)); lgd = legend('line 1','line 2','line 3'); lgd.

How do you omit something from a legend in MATLAB?

1- Select the curve you don't want have legend. 2- Go to the "more properties" (while the curve is still selected). 3- Turn "HandleVisibility" off.

How do I manually add a legend in MATLAB?

x = linspace(0,10); y1 = sin(x); y2 = sin(0.9*x); y3 = sin(0.8*x); y4 = sin(0.7*x); y5 = sin(0.6*x); y6 = sin(0.5*x); plot(x,y1,'DisplayName','sin(x)') hold on plot(x,y2,'DisplayName','sin(0.9x)') plot(x,y3,'DisplayName','sin(0.8x)') plot(x,y4,'DisplayName','sin(0.7x)') plot(x,y5,'DisplayName','sin(0.6x)') plot(x,y6,' ...


2 Answers

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

like image 89
gnovice Avatar answered Sep 24 '22 14:09

gnovice


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

like image 23
EBH Avatar answered Sep 22 '22 14:09

EBH