在Matlab条形图中对图例颜色进行反向排序 [英] reverse ordering of legend colors in matlab bar plot

查看:638
本文介绍了在Matlab条形图中对图例颜色进行反向排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matlab中尝试更改与条形图相关的某些属性非常令人困惑.我在这里找到了许多解决我问题的方法.但是,有一个我找不到.请考虑以下内容:

Trying to change some of the properties associated with the bar plot in matlab is very confusing. I have found a number of solution to my problems here. However, there is one that I cannot find. Consider the following:

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中生成单个堆叠的条形图,虽然不是最复杂的解决方案,但它可以工作.我得到的条形图如下:

This code was written to produce a single stacked bar in matlab, not the most sophisticated of solutions, but it works. The bar plot I get is below:

现在,我正尝试颠倒我的图例条目的顺序,以使它们与剧情匹配.有人建议在字符串上使用flipud或fliplr,但这是行不通的.这会更改字符串的顺序,但不会更改颜色.谁能建议一种使图例和图之间的颜色顺序匹配的方法?例如,str4应该是蓝色

Now I am trying to reverse the order of my legend entries so that they match up with the plot. Some people have suggested flipud or fliplr on the strings, but this doesnt work. This changes the sequence of the strings but does not change the colors. Can anyone suggest a method that an match up the ordering of the colors between the legend and the plot? For example, str4 should be blue

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

Note that the flipud suggestion works for line plots, but not for a stacked bar plot like this. Example using line plot:

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提供的答案的解决方案:

Here is the solution using the answer provided by 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

推荐答案

因此,您可以独立于条形图的颜色来控制图例的颜色(请注意,我是根据

So you can control the colours of the legend independently of the colors of the bar chart like this (note that I got this idea based on this post and then by inspecting the object properties in the command line):

[ll, ll2] = legend(str);

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

因此,尝试将RGBTriple设置为[1,0,0],您应该看到所有图例框变为红色.因此,现在只是获取条形图颜色(可能以非常相似的方式)并翻转它们的情况.

So try setting RGBTriple to [1,0,0] and you should see all the legend boxes become red. So now it's just a case of getting the bar chart colors (probably in a very similar manner) and flipping them.

好的,所以这是一种找到正确颜色的简单方法:

OK so here is a hacky way to find the right colors:

  1. 获取当前的颜色图:

  1. Get the current colormap:

map  = colormap;

  • 将颜色图缩小为图例的大小:

  • Reduce the colormap to the size of your legend:

    MAP = map(linspace(size(map,1),1,n/2),:); %// n is as my code above
    

  • 将其用于RGBTriple:

  • Use it for RGBTriple:

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

  • 这篇关于在Matlab条形图中对图例颜色进行反向排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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