matlab条形图中图例颜色的逆向排序

matlab条形图中图例颜色的逆向排序,matlab,Matlab,试图在matlab中更改与条形图相关的一些属性是非常令人困惑的。我在这里找到了许多解决问题的方法。然而,有一个我找不到。考虑以下事项: ax1 = subplot(121); id2 = [8;2;3;5]; id2_t = sum(id2); id3 = (id2/id2_t).*100; id3(:,2) = 0; H1 = bar(id3','stacked','EdgeColor','none'); set(gca,'XTicklabel',[]); xlim([0.75 1.25]);

试图在matlab中更改与条形图相关的一些属性是非常令人困惑的。我在这里找到了许多解决问题的方法。然而,有一个我找不到。考虑以下事项:

ax1 = subplot(121);
id2 = [8;2;3;5];
id2_t = sum(id2);
id3 = (id2/id2_t).*100; id3(:,2) = 0;
H1 = bar(id3','stacked','EdgeColor','none');
set(gca,'XTicklabel',[]);
xlim([0.75 1.25]);

% add legend
str = {'str1','str2','str3','str4'};
ll = legend(str);
legend('boxoff');
set(ll,'PlotBoxAspectRatio',[0.5 1 1]);
ll_i = get(ll,'position');
set(ll, 'Position', [0.25 ll_i(2)-0.1 ll_i(3) ll_i(4)]);

% change dimensions of plot
AX1 = get(ax1,'position');
set(ax1,'position',[AX1(1) AX1(2) AX1(3)/2.7 AX1(4)]);
这段代码是为了在matlab中生成单个堆叠条而编写的,虽然不是最复杂的解决方案,但它可以工作。我得到的条形图如下:

现在我正试图颠倒我的图例条目的顺序,以便它们与情节匹配。有些人建议在弦上使用flipud或fliplr,但这不起作用。这会更改字符串的顺序,但不会更改颜色。有人能提出一种方法来匹配图例和情节之间的颜色顺序吗?例如,str4应该是蓝色的

请注意,flipud建议适用于直线图,但不适用于这样的堆叠条形图。使用线图的示例:

x = 1:10;
h = zeros(5, 1);
hold on;
cols = {'r', 'g', 'b', 'y', 'k'};
for k = 1:5
    h(k) = plot(x, k*x, cols{k});
end
legend({'one','two','three', 'four', 'five'}) % one way
legend(flipud(h), {'one', 'two', 'three', 'four', 'five'}) % another way
解决方案

下面是使用Dan提供的答案的解决方案:

ax1 = subplot(121);
id2 = [8;2;3;5];
id2_t = sum(id2);
id3 = (id2/id2_t).*100; id3(:,2) = 0;
H1 = bar(id3','stacked','EdgeColor','none');
set(gca,'XTicklabel',[]);
xlim([0.75 1.25]);

% add legend
str = {'str1','str2','str3','str4'};
[ll ll2]= legend(str);
legend('boxoff');
set(ll,'PlotBoxAspectRatio',[0.5 1 1]);
ll_i = get(ll,'position');
set(ll, 'Position', [0.25 ll_i(2)-0.1 ll_i(3) ll_i(4)]);

% change dimensions of plot
AX1 = get(ax1,'position');
set(ax1,'position',[AX1(1) AX1(2) AX1(3)/2.7 AX1(4)]);

map = colormap;
n = size(ll2,1);
MAP = map(linspace(size(map,1),1,n/2),:); %// n is as my code above
for k = (n/2 + 1):n;
    a1 = get(ll2(k),'Children');
    set(a1,'FaceColor',MAP(k-n/2,:));
end

因此,您可以像这样独立于条形图的颜色来控制图例的颜色(请注意,我是根据命令行中的对象属性并通过检查这些属性得出这个想法的):

因此,尝试将
RGBTriple
设置为
[1,0,0]
,您会看到所有图例框都变成红色。因此,现在只需要获取条形图的颜色(可能以非常类似的方式)并翻转它们

好的,这里有一个简单的方法来找到合适的颜色:

  • 获取当前颜色映射:

    map  = colormap;
    
  • 将颜色贴图缩小到图例的大小:

    MAP = map(linspace(size(map,1),1,n/2),:); %// n is as my code above
    
  • 将其用于RGBTriple:

    for k = (n/2 + 1):n
        ll2(k).Children.FaceColor = MAP(k-n/2,:);
    end
    

  • ll2(k).Children.FaceColor=MAP(k-n/2,:);非结构对象的结构赋值。首先,
    k
    的值是多少?其次,
    ll2(k).Children.FaceColor
    的值是多少?还有什么是
    ll2
    ?我的是一个数组,元素的前半部分包含
    文本
    对象,后半部分包含
    对象赋值也有效吗?i、 e.ll2(k).Children.FaceColor=[1,0,0]?这将使图例all redll(2)成为一个8 x 1的双数组。当然“.”意味着一种结构。ll2(5).Children.FaceColor=[1,0,0];结构分配给非结构对象。为了确保这一点,您将
    ll2
    定义为
    legend
    的第二个输出,即
    [ll,ll2]=legend(str)
    ?也要检查
    ll2
    而不是
    ll(2)
    的值,尽管我确信这是您在运行代码时所做的,但我得到了错误:
    使用matlab.graphics.illustration.Legend/set时出错。
    Legend类上没有PlotBoxAspectRatio属性。
    。所以我怀疑这是版本不兼容(我运行的是2015a),这可能会影响我下面为您工作的答案。但是试试看。
    for k = (n/2 + 1):n
        ll2(k).Children.FaceColor = MAP(k-n/2,:);
    end