Creating a grouped bar plot from a table having multiple column
조회 수: 8 (최근 30일)
이전 댓글 표시
I have the following table:
ValuationRatios company industry sector
___________________________________ _______ ________ ______
"P/E Ratio" 31.96 0 0
"Price/Revenue" 7.79 6.35 7.04
"Price/Book" 45.88 25.45 10.02
"Price to Cash Flow per Share" 28.46 1.33 3.89
"Price to Free Cash Flow per Share" 31.66 1.82 6.37
"Dividend Yield" 0.49 0.49 0.6
"Payout Ratio" 15.58 15.59 28.8
"Quick/Acid Test Ratio" 0.75 0.61 1.03
whos Tindclean
Name Size Bytes Class Attributes
Tindclean 23x4 4221 table
Tindclean.Properties.VariableTypes
string double double double (1 x 4) string
I am trying to create a grouped bar plot/stacked from the table.
댓글 수: 0
답변 (2개)
dpb
2025년 7월 29일
VNames=["ValuationRatios","company","industry","sector"];
VR=[
"P/E Ratio"
"Price/Revenue"
"Price/Book"
"Price to Cash Flow per Share"
"Price to Free Cash Flow per Share"
"Dividend Yield"
"Payout Ratio"
"Quick/Acid Test Ratio"];
Data=[
31.96 0 0
7.79 6.35 7.04
45.88 25.45 10.02
28.46 1.33 3.89
31.66 1.82 6.37
0.49 0.49 0.6
15.58 15.59 28.8
0.75 0.61 1.03 ];
tData=[table(VR) array2table(Data)];
tData.Properties.VariableNames=VNames;
tData=convertvars(tData,'ValuationRatios','categorical')
bar(tData.ValuationRatios,tData{:,2:end})
Cris LaPierre
2025년 7월 29일
Tindclean = readtable('Tindclean.xlsx')
% Create a stacked barplot of table Tindclean
barData = Tindclean{:, 2:end}; % Assuming the first column is categorical
barLabels = Tindclean{:, 1}; % Assuming the first column contains labels
figure;
bar(barData, 'stacked');
set(gca, 'XTickLabel', barLabels);
xlabel('Categories');
ylabel('Values');
title('Stacked Bar Plot of Tindclean');
legend(Tindclean.Properties.VariableNames(2:end), 'Location', 'bestoutside');
댓글 수: 4
Cris LaPierre
2025년 7월 30일
Possible without a for loop. However, the challenge now is how to indicate which bar corresponds to which axis.
Tindclean = readtable('mydata.xls');
Tindclean.ValuationRatios = categorical(Tindclean.ValuationRatios);
% Create a stacked barplot of table Tindclean
barData = Tindclean{:, 2:end}; % Assuming the first column is categorical
barLabels = Tindclean{:, 1}; % Assuming the first column contains labels
figure;
yyaxis left
colororder("gem")
ind = contains(string(barLabels),'Employee');
bar(barLabels(~ind),barData(~ind,:), 'stacked');
xlabel('Categories');
ylabel('Values');
yyaxis right
colororder("gem")
bar(barLabels(ind),barData(ind,:), 'stacked');
title('Stacked Bar Plot of Tindclean');
legend(Tindclean.Properties.VariableNames(2:end), 'Location', 'bestoutside');
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





