MATLAB Answers

0

how to manually calculate the stacked bar

Omar Alamoudi さんによって質問されました 2019 年 6 月 10 日
最新アクティビティ dpb
さんによって コメントされました 2019 年 6 月 12 日
dpb
さんの 回答が採用されました
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!!

  0 件のコメント

サインイン to comment.

1 件の回答

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 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
2019 年 6 月 12 日
That's a good thought/modifcation, too...

サインイン to comment.



Translated by