Creating a grouped bar plot from a table having multiple column

조회 수: 8 (최근 30일)
Prashant joshi
Prashant joshi 2025년 7월 29일
댓글: Cris LaPierre 2025년 7월 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.

답변 (2개)

dpb
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')
tData = 8×4 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
bar(tData.ValuationRatios,tData{:,2:end})
  댓글 수: 2
Prashant joshi
Prashant joshi 2025년 7월 29일

Thanks. I appreciate it. Perfect! I have follow up question, if I do not have to type variable names ( some data have several hundred variables) is there any way to tackle that?

Prashant joshi
Prashant joshi 2025년 7월 29일
it looks like there is a scaling issue. There are some variables with different scales than others and i do not want to normalize them. can you please help? my data file is attached.

댓글을 달려면 로그인하십시오.


Cris LaPierre
Cris LaPierre 2025년 7월 29일
Tindclean = readtable('Tindclean.xlsx')
Tindclean = 8×4 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
% 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
Prashant joshi
Prashant joshi 2025년 7월 29일

Thanks. That will work.

Can we set like yyaxis right yyaxis left may be for loop but I am not able to do that with the plot not sure if we can do it. Any insight is helpful.

Cris LaPierre
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 CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by