【问题标题】:reverse ordering of legend colors in matlab bar plotmatlab条形图中图例颜色的反向排序
【发布时间】:2015-07-02 07:21:49
【问题描述】:

试图在 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 应该是蓝色的

请注意,翻转建议适用于线图,但不适用于像这样的堆积条形图。使用线图的示例:

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

【问题讨论】:

  • 当我运行你的代码时,我得到了错误:Error using matlab.graphics.illustration.Legend/setThere is no PlotBoxAspectRatio property on the Legend class.。所以我怀疑这是版本不兼容(我正在运行 2015a),这可能会影响我在下面为您工作的答案。但是试试看。

标签: matlab


【解决方案1】:

因此,您可以像这样独立于条形图的颜色来控制图例的颜色(请注意,我基于this post 得到了这个想法,然后通过检查命令行中的对象属性):

[ll, ll2] = legend(str);

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

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

好的,这里有一个找到正确颜色的 hacky 方法:

  1. 获取当前颜色图:

    map  = colormap;
    
  2. 将颜色图缩小到图例的大小:

    MAP = map(linspace(size(map,1),1,n/2),:); %// n is as my code above
    
  3. 将它用于 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?我的是一个数组,前半部分元素包含Text 对象,后半部分包含Group 对象
  • 分配静态值也有效吗?即 ll2(k).Children.FaceColor = [1,0,0]?那应该使图例全部变红
  • ll(2) 是一个 8 x 1 双精度数组。当然是'。暗示结构。 ll2(5).Children.FaceColor = [1,0,0];结构分配给非结构对象。
  • 为了确定,您将ll2 定义为legend 的第二个输出,即[ll, ll2] = legend(str)?还要检查 ll2 而不是 ll(2) 的值,尽管我确定这就是你所做的
猜你喜欢
  • 1970-01-01
  • 2019-06-01
  • 2018-04-04
  • 2018-10-28
  • 2015-02-03
  • 2012-06-13
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多