我需要使用grouped
样式绘制水平条,以便属于每个组的所有条具有相同的颜色但与其他组不同(即顶部组的所有条都是红色,其下方的组 - 绿色,等等.. .)
另外,如何将值水平放置在每个条形的顶部?我怎样才能控制这些值的位置?
这是我的代码:
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');
这是我现在的图:
这是一个技巧:一次
绘制 1 行的条形图,并用NaN
s填充空间,同时为所有行指定一种颜色。绘图NaN
s 将不绘制任何内容。
[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)
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句