如何使成组的单杠具有相同的颜色? [英] How to make groups of horizontal bars have the same color?

查看:90
本文介绍了如何使成组的单杠具有相同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用grouped样式绘制水平条,以使属于每个组的所有条具有相同的颜色但与其他组不同(即,顶部组中的所有条均为红色,下方组为绿色) ,依此类推...).

I need to plot horizontal bars using the grouped style such that all the bars belonging to each group have the same color but different from other groups (i.e. all bars of the top group are red, the group below it - green, and so on...).

此外,如何将这些值水平放置在每个条形的顶部?如何控制这些值的位置?

Also, how can I put the values on the top of each bar horizontally? How can I control the position of such values?

这是我的代码:

y = [91.9 8.1 94.4 5.6; 84.9 15.1 90.12 9.88; 89.4 10.6 91.2 8.8; 72 28 50.9 49.1];

h = barh(y,'grouped'); 
job = {'group1','group2 ','group 3','group4'};
legend(job,'location','northeast');

这是我目前的数字:

推荐答案

这是黑客:
一次绘制一行的条形图,并用NaN填充空间,同时为所有行指定一种颜色.绘制NaN不会绘制任何内容.

Here's a hack:
Plot bar graph for 1 row at a time and fill the space with NaNs while specifying a single color for all rows. Plotting NaNs will plot nothing.

[ry, cy] = size(y);   %Number of rows and columns of y
hold on;
for k = 1:ry
    tmp = [NaN(k-1,cy); y(k,:); NaN(4-k,cy)];   %Filling with NaNs
    h{k} = barh(tmp, 'FaceColor', rand(1,3));   %Plotting bar graph in random color
    h{k} = h{k}(1);   %Store handle of any of the rows (Required for legend)
end
job = {'group1', 'group2', 'group3', 'group4'}; %Required legend entries
legend([h{:}], job);  %Default location is already 'northeast' (so can be skipped)

输出:

这篇关于如何使成组的单杠具有相同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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