[英]How to add an independent text in MATLAB plot legend
我需要图例中与图形数据无关的附加文本以及图例标题。 像这样的东西(它是在OriginLab中制作的):
在此链接之后添加与图形无任何关系的自定义图例我可以使用
plot(NaN,NaN,'display','add info here2', 'linestyle', 'none')
添加一些文本plot(NaN,NaN,'display','add info here2', 'linestyle', 'none')
。 但是本文中有一个缩进:
如何避免这种缩进? 是否有更优雅的方法来添加与图例无关的文本以及图例标题?
legend
函数将作为其第二个输出参数句柄处理,以处理构成图例中符号和文本的所有组件。 因此,您可以在图例中将“虚拟”线条绘制为占位符,在创建图例时对手柄重新排序以将文本放置在所需位置,并相应地修改图例对象。 这是一个例子:
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
您可以使用注释。 它并不完美,但只需很少的调整就能得到你想要的东西。 这是一个例子:
% 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');
从中你得到:
我认为最简单的方法是创建一些虚拟函数,绘制它但设置color =“none” - 这样它只会出现在图例中(如果那是你想要它的地方)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.