Creating a grouped bar plot from a table having multiple column

7 vues (au cours des 30 derniers jours)
Prashant joshi
Prashant joshi le 29 Juil 2025
Commenté : Cris LaPierre le 30 Juil 2025
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.

Réponses (2)

dpb
dpb le 29 Juil 2025
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 commentaires
Prashant joshi
Prashant joshi le 29 Juil 2025

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 le 29 Juil 2025
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.

Connectez-vous pour commenter.


Cris LaPierre
Cris LaPierre le 29 Juil 2025
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 commentaires
Prashant joshi
Prashant joshi le 29 Juil 2025

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 le 30 Juil 2025
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');

Connectez-vous pour commenter.

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by