Overlay and group two (or more) bar graphs (value by value)
4 ビュー (過去 30 日間)
古いコメントを表示
Hi. I need to create a graph by overlapping and grouping the values on the vertical axis of two graphs I have generated:
load CountArray_A
load CountArray_S
x_A = CountArray_A(:,1).';
y_A = CountArray_A(:,2);
figure();
graph_A = barh(x_A,y_A,'FaceColor',[1 0 0],'EdgeColor','none');
name_A = "analysis A";
legend({name_A},'Location','northeast','Orientation','horizontal')
x_S = CountArray_S(:,1).';
y_S = CountArray_S(:,2);
figure();
graph_S = barh(x_S,y_S,'FaceColor',[0 1 0],'EdgeColor','none');
name_S = "analysis S";
legend({name_S},'Location','northeast','Orientation','horizontal')
The overall figure I would like to get should look like this (applied to my case, of course):

p.s. It could also happen, I don't know if this is a case, that a value present on the vertical axis is not present in one graph (but present in the other). In this case there would be only one bar (and not two) at the number present on the vertical axis.
0 件のコメント
採用された回答
Star Strider
2023 年 8 月 2 日
Try this —
LD1 = load('CountArray_A.mat');
LD2 = load('CountArray_S.mat');
CountArray_A = LD1.CountArray_A;
CountArray_S = LD2.CountArray_S;
C(:,1) = (min([CountArray_A(:,1); CountArray_S(:,1)]) : max([CountArray_A(:,1); CountArray_S(:,1)])).';
C(:,[2 3]) = zeros(size(C(:,1),1),2);
LvA = ismember(C(:,1), CountArray_A(:,1));
LvS = ismember(C(:,1), CountArray_S(:,1));
C(LvA,2) = CountArray_A(:,2);
C(LvS,3) = CountArray_S(:,2);
figure
hbh = barh(C(:,1), C(:,[2 3]));
cm = [1 0 0; 0 1 0];
for k = 1:numel(hbh)
hbh(k).FaceColor = cm(k,:);
hbh(k).EdgeColor = 'none';
end
legend('Analysis A', 'Analysis S', 'Location','best')
It first creates a common ‘x’ vector and then fills the last two columns of the ‘C’ matrix with appropriate values from the two ‘count’ vectors using a ‘logical indexing’ approach with the logical indices created by the ismember calls.
.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!