Bar Graph Stacked with Negative and Positive values

91 ビュー (過去 30 日間)
Brishen
Brishen 2011 年 10 月 7 日
編集済み: Koorosh Aslansefat 2020 年 7 月 17 日
Hi.
I'm trying to get a plot to act like the graph function in excel that stacks values together. It does this by putting all positive values on the positive part of the axis, and the negative values on the negative side.
Unfortunatly when I use bar(X,'stack') with matlab it doesn't do this.
Is there any way to get this same functionality? Thanks.

採用された回答

Brishen
Brishen 2011 年 10 月 7 日
I think that I figured this out. The trick is to do two graphs and to seperate out the positive and negative values:
>> X = rand(4,3) - 0.5;
>> Xneg = X;
>> Xneg(Xneg>0) = 0;
>> Xpos = X;
>> Xpos(Xpos<0) = 0;
>> hold on
>> bar(Xneg,'stack')
>> bar(Xpos,'stack')
>> hold off

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 10 月 7 日
It isn't clear to me that what you describe would be appropriate? 'stacked' means to show the cumulative sum with internal divisions. If some members of the bar are positive and others are negative, then if you have a break at the axis then you misrepresent the cumulative sum.
  2 件のコメント
Brishen
Brishen 2011 年 10 月 7 日
I agree with what you're saying, but I'd still like to be able to have this function in a graph. The reason is that there are times when you have data that fall into the same category but can be both negative and positive. Although the net number is interesting it can also be important to see the sums of both the signs and to see how much each row contributed to it.
If stacked isn't the best solution here then I'm completely open to another.
Thanks.
Walter Roberson
Walter Roberson 2011 年 10 月 7 日
Something like this, perhaps:
t1 = X;
t2 = X;
t1(t1<0) = nan;
t2(t2>0) = nan;
Xsplit = reshape([t2.';t1.'],size(t,2),[]).';
bar(Xsplit,'stacked')

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


Koorosh Aslansefat
Koorosh Aslansefat 2020 年 7 月 17 日
編集済み: Koorosh Aslansefat 2020 年 7 月 17 日
I would suggest to try this:
bar(X,'BaseValue',0)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by