フィルターのクリア

merge three barh (stacked)

3 ビュー (過去 30 日間)
Rachele Franceschini
Rachele Franceschini 2022 年 4 月 12 日
コメント済み: Mathieu NOE 2022 年 4 月 13 日
I have one table with different regions and data. Each color outlines different data that it should be one bar.
For example the variable A has one bar (stacked) with values from column "B" to "K". Then always the variable A has the bar (stacked) from column "L" to "U" and at the end the third bar with value from "V" to "AE".
t = readtable('Cartel2.xlsx')
I tried this code, but it is not clear, too long
x = [1 2 3];
y = [0.18 0.54 0.50 0.74 0.30 2.18 0.64 3.75 0.56 0.62; 0.63 1.10 1.39 1.86 1.59 0.71 0.67 0.68 0.74 0.63; 4.12 0.00 0.08 0.00 0.05 1.45 0.00 0.00 0.00 0.30];
barh(x,y,'stacked')
I would like to get bar (stacked) sequential, it is possible?

採用された回答

Voss
Voss 2022 年 4 月 12 日
Is this what you have in mind?
(For each variable A through V, one bar with the sum of columns B - K (always 10 in this file), one bar with the sum of columns L - U (also always 10 in this file), one bar with the sum of columns V - AE.)
M = readcell('Cartel2.xlsx');
names = M(:,1);
N = numel(names);
M = cell2mat(M(:,2:end));
M_sum = [ ...
sum(M(:,1:10),2) ...
sum(M(:,11:20),2) ...
sum(M(:,21:30),2) ...
];
barh(1:N,M_sum,'stacked');
set(gca(),'YLim',[0 N+1],'YTick',1:N,'YTickLabel',names,'YDir','reverse');
  2 件のコメント
Rachele Franceschini
Rachele Franceschini 2022 年 4 月 12 日
without sum. I would like three barh for each variable in y ( A, B, C, etc). Each barh has values as in table.
For example in this way.
Voss
Voss 2022 年 4 月 12 日
編集済み: Voss 2022 年 4 月 12 日
M = readcell('Cartel2.xlsx');
names = M(:,1);
N = numel(names);
M = cell2mat(M(:,2:end));
hold on
for ii = 1:N
barh(ii+[-0.25 0 0.25],reshape(M(ii,:),[],3).','stacked');
end
set(gca(), ...
'YLim',[0 N+1], ...
'YTick',1:N, ...
'YTickLabel',names, ...
'YDir','reverse', ...
'XLim',[0 10.4], ...
'Box','on', ...
'TickDir','out');

サインインしてコメントする。

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2022 年 4 月 12 日
hello Rachele
try this !
you get now one figure (plot) per variable , 20 in total)
all the best
t = readcell('Cartel2.xlsx');
var_names = t(:,1);
data = cell2mat(t(:,2:end)); % 3 groups of 10 rows
for ci = 1:numel(var_names)
y = data(ci,:);
yy = reshape(y,3,10);
figure(ci),
barh(x,yy,'stacked')
title(['variable :' var_names(ci) ]);
end
  4 件のコメント
Rachele Franceschini
Rachele Franceschini 2022 年 4 月 12 日
Thank you! I like also this solution! Thank for your help!
Mathieu NOE
Mathieu NOE 2022 年 4 月 13 日
My pleasure !

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by