Matlab:保存后翻转图例顺序和图例重叠图

Arg*_*gon 5 matlab

我试图根据matlab barplot 中图例颜色的反向排序来反转我的图例条目顺序,但它似乎不适用于我的情况。

基本上我拥有的是一个 GUIDE 图,它绘制了很多图并能够将它们保存到 .png 文件中。效果如下: http://i.imgbox.com/KzEFAdia.png

我设法通过颠倒图例来更改文本顺序,但无法更改图例颜色顺序。这是我所得到的:

[a b] = legend(legenda);

map = colormap; % current colormap

n = size(b,1);

z = linspace(size(map,1),1,n/3); % there is 1 text and 2 line elements for every data series, so I divide by 3

z = round(z); %otherwise matlab gets angry that indices must be real integers or logicals

MAP = map(z(:),:); % gets elements specified by linspace from colormap
Run Code Online (Sandbox Code Playgroud)

到目前为止一切正常。

两个系列的 b 向量如下所示(从 2.0 开始,因为它是相反的):

Text    (P+C 200 2.0.dpt)
Text    (P+C 200 1.0.dpt)
Line    (P+C 200 2.0.dpt)
Line    (P+C 200 2.0.dpt)
Line    (P+C 200 1.0.dpt)
Line    (P+C 200 1.0.dpt)
Run Code Online (Sandbox Code Playgroud)

所以我发现(基于链接的代码),我必须更改每个行条目的颜色变量。

for k = (n/3 + 1):n
   a1 = get(b(k),'Children');
   set(a1,'FaceColor',MAP(ceil((k - n/3)/2), :));
end
Run Code Online (Sandbox Code Playgroud)

向上取整并除以 2 会得到两次相同的索引。

然而,这段代码什么也不做。

我已经检查过翻转图例向量是否可能是问题的根源,但颜色顺序保持不变。我也尝试过使用 MAP 矢量 - 没有运气。

当我删除 for 循环中 a1 = ... 行后面的分号时,我得到:

a1 = 

  0x0 empty GraphicsPlaceholder array.
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它发挥作用?

另外,有没有什么好方法可以让图例在保存后不覆盖绘图(参见上面链接的图片)?

我保存它的方法是创建一个带有“可见”“关闭”的临时图形,并复制轴和图例,然后保存。否则它会保存整个数字。

il_*_*ffa 5

在您的情况下,在matlab 条形图中反向排序图例颜色的答案中提供的代码不起作用的原因是,在这种情况下(图表的bar图)图例中的对象是,patches而在您的图中它们是lines

FaceColor适用于补丁,不适用于lines.

解决问题的最简单方法应该是颠倒“从一开始”绘制线条的顺序,并直接使用从colormap.

legend 然而,如果您想在绘制图形后使用,除了反转中的项目之外,如果您想使用从(目前)legend中提取的颜色集,您还必须更改图中线条的颜色colormap在你的图片中,有些线条具有相同的颜色)。

可以分两步解决问题:

  • 恢复图例中的字符串并更改绘图中线条的颜色
  • 相应地更新图例中线条的颜色

这两个步骤是必需的,因为当您更改图中线条的颜色时,图例中的项目会自动更新。

参考您发布的代码:您可以通过 array 访问图例的字符串和线条的颜色b

您可以访问绘制的线条的手柄,如下所示:

p_h=get(gca,'children')
Run Code Online (Sandbox Code Playgroud)

由于您绘制了 10 条线,因此数组b的构建如下:

  • b(1:10) 包含string
  • b(11:2:30) 包含lines
  • b(12:2:30) 包含markers

要改变图例的位置,您可以设置其location属性:要将其放在外面,axes您可以将其设置为:

    'NorthOutside'       outside plot box near top
    'SouthOutside'       outside bottom
    'EastOutside'        outside right
    'WestOutside'        outside left
    'NorthEastOutside'   outside top right (default for 3-D plots)
    'NorthWestOutside'   outside top left
    'SouthEastOutside'   outside bottom right
    'SouthWestOutside'   outside bottom left
Run Code Online (Sandbox Code Playgroud)

在下面您可以找到已实现上述建议的代码。

figure
% Initial plot
h_p=plot(0:.1:2*pi,bsxfun(@plus,sin([0:.1:2*pi]),[3:3:30]'),'linewidth',3)
% Initial legend
[h_leg,b]=legend(cellstr(strcat(repmat('sin(x)+',10,1),num2str([3:3:30]'))))
%
% YOUR CODE TO GENERATE NTHE NEW COLORS
%
map = colormap; % current colormap
n = size(b,1);
z = linspace(size(map,1),1,n/3); % there is 1 text and 2 line elements for every data series, so I divide by 3
z = round(z); %otherwise matlab gets angry that indices must be real integers or logicals
MAP = map(z(:),:); % gets elements specified by linspace from colormap
%
% Reverse the legend strings
%
rev_str=flipud(get(b(1:10),'string'))
%
% Get the handles of the lines in the legend
%
b1=b(11:2:30)
%
% Revere the string in the legend
% and update the color of the lne in the plot using the colors defined in
% MAP
%
p_h=get(gca,'children')
for i=1:10
   set(b(i),'string',rev_str{i})
   set(p_h(i),'color',MAP(i,:),'linewidth',3)
end
%
% Reverse the color of the lines in the legend
for i=1:10
   set(b1(i),'color',MAP(i,:),'linewidth',3)
end
%
% Move the legend outside the axes
%
set(h_leg,'location','NorthEastOutside')
Run Code Online (Sandbox Code Playgroud)

原作剧情

在此输入图像描述

更新剧情

在此输入图像描述

希望这可以帮助。