How to add independent text to MATLAB story legend

I need additional text in a legend that is not associated with graphic data along with legend inscriptions. Something like this (it was done in OriginLab):

enter image description here Following this link Add a custom legend without any relation to the graph I can add 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 indentation? And is there a more elegant way to add text that is not associated with the legend along with the legends?

+5
source share
4 answers

The legend function will return as the second output argument for all components that make up the characters and text in the legend. Therefore, you can build dummy strings as placeholders in the legend, change the order of the pens when creating the legend, to place the text where you want, and change the legend objects accordingly. Here is 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

+4
source

You can use annotations. This is not ideal, but with a few adjustments you will get what you want. Here is 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

+2
source

You can simply add any text to any point in the chart this way:

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

where x1, y1 are the coordinates.

enter image description here

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

+1
source

I think the easiest way is to simply create some dummy function, capture it, but set the color = "no" - this way it will only be displayed in the legend (if that’s exactly what you need).

0
source

Source: https://habr.com/ru/post/1263199/


All Articles