Creating a grouped bar plot from a table having multiple column

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.

Respuestas (2)

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 comentarios

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?

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.

Iniciar sesión para comentar.

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 comentarios

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?
% 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');
If you don't want to normalize, then what strategy would you propose?
About the only option that I can think of, then, is to use logarithimic Y 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;
bar(barLabels,barData, 'stacked');
xlabel('Categories');
ylabel('Values');
title('Stacked Bar Plot of Tindclean');
legend(Tindclean.Properties.VariableNames(2:end), 'Location', 'bestoutside');
yscale('log')

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.

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');

Iniciar sesión para comentar.

Categorías

Más información sobre Discrete Data Plots en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Jul. de 2025

Comentada:

el 30 de Jul. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by