how to manually calculate the stacked bar

1 回表示 (過去 30 日間)
Omar Alamoudi
Omar Alamoudi 2019 年 6 月 10 日
コメント済み: dpb 2019 年 6 月 12 日
Hello everyone,
I am struggling to understand how MATLAB is calculating its 'stacked' bar plots.
In my experiment, I take the difference of several vectors and I end up with positive and negative values. then I plot them as 'stacked' bars. I just want to understand how MATLAB reached to that plot because I could not do the computation myself. in their help document they say:
"If y is an m-by-n matrix, then bar displays m bars where each bar height is the sum of the elements in the row. Each bar is multicolored. Colors correspond to distinct elements and show the relative contribution each row element makes to the total sum"
How are they showing that bar height is the sum of all the elements and at the same time show the relative contribtion of each elements that makes to the total sum???!!!
I want to be able to manually calculate the stacked bar myself then I'll plot them as a normal plot as a one color because I am more interested in the net rather than the individual contribution. also, I'll be able to add errorbars and do more statisitics.
an example:
N = [0.104289664611676, -0.328634522539032, 0.534512715012202, -0.115290712257105, -0.0873978289502759, -0.310172844013392, -0.101568169901694, 0.0782078439567624, 0.226053854080858 ; 0.104853556666576, 0.0277767793523505, 0, 0, 0, -0.0277767793523505, -0.00257839874920164, -0.102275157917374, 0];
P = [-0.359650600161484, -0.00882683924118048, 0.151008014196010, 0.214974668975995, -0.120598651574417, -0.0425338382042942, -0.232471668177692, -0.0686700596309392, 0.466768973818002 ; 0, 0.166302832327649, 0, 0, 0.0690888201010693, -0.0769324569974460, -0.289045619213330, 0.351763782126669, -0.221177358344612];
figure;
subplot(1,2,1)
bar(N,'stacked')
subplot(1,2,2)
bar(P,'stacked')
Thanks!!

採用された回答

dpb
dpb 2019 年 6 月 11 日
It's hard to do with mixed signed numbers because they end up on top of each other...to see more clearly what it actually does, try
bar(abs(P),'stacked')
so all values are positive and then can see all the segments without any being occluded.
It's just cumsum() of the data is the total, when start with zero baseline,then the first is negative and the second sum if positive is inside that already drawn area.
bar is probably not the way to try to show such data
ADDENDUM
Alternatively, to help see somewhat more clearly, you can try
bar(P,'stacked','basevalue',min(P(:)))
which won't fix all the display issues but some more detail wil be visible.
  2 件のコメント
Omar Alamoudi
Omar Alamoudi 2019 年 6 月 12 日
Thank you @dpb for your quick and clear answer!
One of the things that made it clear for me, as well, after reading more is that if you make the bar plot more transparent you will be able to see the overlaping:
figure
bar(P,'stacked')
alpha(0.4)
dpb
dpb 2019 年 6 月 12 日
That's a good thought/modifcation, too...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by