Bar plot with two axis no plotting as grouped

4 ビュー (過去 30 日間)
HWIK
HWIK 2021 年 8 月 30 日
コメント済み: HWIK 2021 年 8 月 30 日
Hi,
I want to plot a bar plot with two axis, and I want the two y vars to be grouped, but they always come out as stacked. I attached some sample code that you will need to execute the following code:
yyaxis left
bar(t,a,'grouped')
hold on
yyaxis right
bar(t,b,'grouped')
The output I get is the following:
Any idea why this might be happening?
Any help is appreciated,
Thanks!

採用された回答

dpb
dpb 2021 年 8 月 30 日
They're not stacked, they're just plotted on top of each other because there's only one series on each call to bar(); hence there's nothing to group. You just see whichever is the taller and if it happens to be the RH axis that is the top layer, then you see both which gives the appearance of stacked--but you'll note the value isn't the total as would be for a real stacked bar.
A diagnostic warning message about using the 'stacked' option with only one series might be a useful enhancement to give the user a clue what's going on.
Try
yyaxis left
bar(t,[a nan(size(a))],'grouped')
yyaxis right
bar(t,[nan(size(b)) b],'grouped')
The "trick" of a NaN placeholder array is a very common one and is often necessary to get the desired result with handle graphics as here.
  1 件のコメント
HWIK
HWIK 2021 年 8 月 30 日
That is a very clever trick. Very much appreciated, thank you!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by