如何在图形的图例中添加额外的信息? [英] How to add extra information to figure's legend?

查看:88
本文介绍了如何在图形的图例中添加额外的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向MATLAB中的图形添加额外的信息,如下所示:

I want to add extra Information to a figure in MATLAB, like this:

有可能吗? (我当然想让它更漂亮)

Is it possible? (Of course I want it to be more beautiful)

推荐答案

我整理了一些合理通用的东西,请参见下文.

I put together something reasonably generic, see below.

我将对此进行更多的概括,并将其发布到文件交换中,我认为它是一个相当不错的工具:)

I will generalize this a bit more and post it to the File Exchange, I think it's a fairly nice tool to have around :)

我打算

  • 自动调整表格大小以适合其内容
  • 使其适合于任意图例放置
  • 用鼠标移动表和图例时将它们耦合在一起
  • 将标题设为可选
  • 支持各种类型的输入

但是现在:

% Example input

plot(1,1,'r.', 1.1,1.1', 'b.', 1.2,1.2, 'k.');
legendHandle = legend('plot 1', 'plot 2 with longer title', 'plot 3');

tableHead = {'\theta_0' '\phi' 'df/dx'};
tableContent = rand(3);




% Extract information 
legendPosition = get(legendHandle, 'position');
children = get(legendHandle, 'children');
labels   = children(strcmp(get(children, 'type'), 'text'));


% Basic error traps
if size(tableContent,1) ~= numel(labels)
    error('LegendTable:dimension_mismatch',...
        'Each legend entry must have a corresponding row in the table.')
end

if size(tableHead,2) ~= size(tableContent,2)
    error('LegendTable:dimension_mismatch',...
        'Table header dimensions are inconsistent with table data.');
end

% Convert header & content to cell-array when necessary
if isnumeric(tableContent)
    tableContent = cellfun(@num2str, ...
        num2cell(tableContent), 'UniformOutput', false);
end
if isnumeric(tableHead)
    tableHead = cellfun(@num2str, ...
        num2cell(tableHead), 'UniformOutput', false);
end

% Proper tick locations for the table
xticks = linspace(0, 1, numel(tableHead)+1);
yticks = linspace(0, 1, numel(labels)+2);

% Text positions are in the centers of the table cells
txt_xPositions = xticks(1:end-1) + (xticks(2)-xticks(1))/2;
txt_yPositions = fliplr(yticks(1:end-1) + (yticks(2)-yticks(1))/2);

% Derive correct table position
headerHeight  = legendPosition(4)/numel(labels);
tablePosition = legendPosition + [0 -headerHeight 0 headerHeight];

% Shift position of original legend 
set(legendHandle, 'position', legendPosition + [-tablePosition(3) -headerHeight 0 0])

% Create table
table = axes(...     
    'position', tablePosition,...
    'xtick', xticks,...
    'ytick', yticks,...
    'xticklabel', [],...
    'yticklabel', [],...    
    'gridlinestyle', '-',...
    'box', 'on',...
    'tag', 'LegendTable');
grid on


% Print table header & table entries
kk = 1;
tableTexts = zeros(numel(tableHead)+numel(tableContent),1);
for ii = 1:numel(txt_xPositions)

    % Column header
    tableTexts(kk) = text(txt_xPositions(ii), txt_yPositions(1), tableHead{ii},...
            'parent', table,...
            'HorizontalAlignment', 'center');
    kk = kk + 1;

    % Column content  
    for jj = 1:numel(txt_yPositions)-1
        tableTexts(kk) = text(...
            txt_xPositions(ii), txt_yPositions(jj+1), tableContent{jj,ii},...
            'parent', table,...
            'HorizontalAlignment', 'center');
        kk = kk + 1;
    end 
end

结果:

这篇关于如何在图形的图例中添加额外的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆