如何向人物的图例添加额外的信息?

问题描述 投票:6回答:4

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

“在此处输入图像描述”

有可能吗? (当然,我希望它更漂亮)

matlab matlab-figure
4个回答
15
投票

我整理了一些[[合理地]]通用名称,请参阅下文。 我将对此进行概括,并将其发布到文件交换中,我认为它是一个相当不错的工具:)

我打算

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

% 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

结果:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8ySUVFMy5qcGcifQ==” alt =“在此处输入图像描述”>“ >>

也许有些矫kill过正:

您可以像这样使用uitable

%define your data: dat = {' green', 1, 2;... ' blue', 2,3.1;... ' yellow', 3, 4.8;} columnname = {' ', 'Param1', 'Param2'}; columnformat = {'char', 'numeric', 'numeric'}; t = uitable('Units','normalized','Position',... [0.05 0.05 0.755 0.87], 'Data', dat,... 'ColumnName', columnname,... 'ColumnFormat', columnformat,... 'RowName',[], 'Parent', gcf);

如果gcf(获取当前图形)不起作用,您只需要知道该句柄!然后,您可以调整大小并做任何您想做的事情,以使其看起来不错...

您可以使用num2str功能将参数传递给图例。

legend(['red'num2str(param1_red)''num2str(param2_red)],...)

有人知道现在是否有更简单的方法来获得相同的结果?像这篇文章一样,我只想要一个普通的图例,在其旁边有一个表,表中带有标题和一些有关该图的额外信息,例如参数设置。请参见图片1,但带有表标题和适当的衬里。

感觉应该有可能,但是广泛的在线搜索和某些尝试并没有带来任何结果。


5
投票
也许有些矫kill过正:

0
投票
您可以使用num2str功能将参数传递给图例。

0
投票
有人知道现在是否有更简单的方法来获得相同的结果?像这篇文章一样,我只想要一个普通的图例,在其旁边有一个表,表中带有标题和一些有关该图的额外信息,例如参数设置。请参见图片1,但带有表标题和适当的衬里。
© www.soinside.com 2019 - 2024. All rights reserved.