フィルターのクリア

How to combine stacked bar chart and line chart?

23 ビュー (過去 30 日間)
Truong Nhu
Truong Nhu 2021 年 2 月 9 日
コメント済み: dpb 2021 年 6 月 23 日
I'm finding a way to combine a stacked bar chart and line chart look like this:
I have tried to use 2-axes graph in (https://www.mathworks.com/help/matlab/creating_plots/overlay-line-plot-on-bar-graph-using-different-y-axes-1.html)
and used the following code:
yyaxis left
b = bar(step,array,'stacked');
yyaxis right
p = plot(step,entropy);
But it seems not to work properly and the result is:
PS: I have 4 components in 1 bar. Sometimes, there's only 1 component presents inside the bar. The line graph works as expected though. Only the stacked bar has problems.
  2 件のコメント
randerss simil
randerss simil 2021 年 2 月 9 日
Can you show the array data ?
Truong Nhu
Truong Nhu 2021 年 2 月 9 日
Yes, of course.
step = 1:1000:5073
array =
0 0 15 0
4 1 10 0
0 0 15 0
4 1 10 0
15 0 0 0
0 0 15 0
PS: In my opinion, matlab doesn't support to plot stacked bar on 2-axes graph. Maybe I need a work-around.

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

採用された回答

dpb
dpb 2021 年 2 月 9 日
Seems to work fine here...
figure
hB=bar(step,array,'stacked');
ylim([0 16])
xlim(xlim+[1 -1]*500)
yyaxis right
hL=plot(step,randi(15,1,6),'*-r');
yielded
The figure you show must have had 10-gazillion x values to have made such a blob.
  3 件のコメント
dpb
dpb 2021 年 2 月 9 日
Hmmm...that's also reproducible here on R2019b; which release are you using?
I had never noticed a similar anomaly with the yyaxis pair before, but I've not used it all that extensively other than here in Answers so don't know that had ever tried a stacked bar plot before.
It isn't unique to the 'stacked' option, either; a regular bar doesn't show different bar colors either as it should.
This appears to be a bug, indeed. Submit a bug report using the "Contact Us" link <ContactTMW Link>
Truong Nhu
Truong Nhu 2021 年 2 月 9 日
I'm using pre-release 2021a. Ok, I'll report this as a bug. Thanks a lot @dpb

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

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 6 月 22 日
編集済み: Cris LaPierre 2021 年 6 月 22 日
I reached out internally. This is the expected behavior, but it's also fairly simple to change this back to what you would have expected by setting the colororder back to 'default'.
step = 1:1000:5073;
array = [0 0 15 0
4 1 10 0
0 0 15 0
4 1 10 0
15 0 0 0
0 0 15 0];
figure
yyaxis left % I added this line to regenerate the error
bar(step,array,'stacked');
ylim([0 16])
xlim(xlim+[1 -1]*500)
% reset colororder
colororder('default')
yyaxis right
plot(step,randi(15,1,6),'*-r');
  4 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 6 月 23 日
編集済み: Cris LaPierre 2021 年 6 月 23 日
Stacked bar plots do, as you've shown above. It is the call to yyaxis that is changing axis properties. Expected behavior means the code is doing what it was written to do. It doesn't mean someone considered this exact scenario and decided this was what people would want. The feedback has been given. My purpose here was to provide a workaround while the feedback is considered.
dpb
dpb 2021 年 6 月 23 日
Ah. A different defintion of "expected"... :) Thanks, Chris.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by