在Matlab中的图例中组合左右文本对齐

时间:2016-04-26 09:08:57

标签: matlab text plot graph legend

我希望我的传奇在Matlab中看起来像这样。

  

[Category1 1.5%]

     

[Category2 2.3%]

左侧调整文本并调整百分比。 有办法做到这一点吗?我正在考虑为图例中的每个条目添加第二个文本对象,但我无法想到这样做的方法。现有的图例文本对象将图例本身作为父图标,但是,我无法向其添加新的文本对象。即使尝试按值复制文本对象也不起作用:

[l_h, object_h] = legend({'A', 'B'});
text_h = findobj(object_h,'Type','Text');

copyobj(text_h(1), text_h(1).Parent)

给了我

  

使用copyobj时出错

     

文字不能是传奇的孩子。

2 个答案:

答案 0 :(得分:1)

您可以使用% demo from legend help: x = 0:.2:12; plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x)); % 3 legend entries which will have "TXT NUMBER%" format text = {'First 10%','Second 20%','Third 30%'}; % make a copy of the text numbers = text; % split the text and numbers into two cell arrays % You probably have them split before here in your % code - I just do it here for demo. for ii=1:length(text) entries = strsplit ( text{ii} ); text{ii} = entries{1}; numbers{ii} = entries{2}; end % find the longest text entry. mlen = max(cellfun ( @length, text )); % create your format string - left justify the text. formatstr = sprintf ( '%%-%is %%s', mlen ); % update the text which will go in the legend for ii=1:length(text) text{ii} = sprintf ( formatstr, text{ii}, numbers{ii} ); end % create the legend h = legend ( text ); % Change the font name - this is important!! h.FontName = 'Courier'; 的技巧,并使用每个字符间距恒定的字体,例如快递:

class ViewController {
protected:
    std::shared_ptr<View> view_;
};

Above code produces the following image

答案 1 :(得分:0)

好的,通过玩游戏,我找到了一种方法来做到这一点。 您无法在图例对象下创建新的Text对象,但是,还有另一个Transform对象用于保存可用于相同目的的图例标记。不确定逻辑是什么,但这有效:

[l_h, object_h] = legend({'A', 'B'});
text_h = findobj(object_h,'Type','Text');
marker_h=findobj(object_h,'Type','Patch');
legend_parent_h = marker_h(1).Parent;

p_h = copyobj(text_h(i), legend_parent_h);    

% now anything can be done with p_h