MATLAB Answers

How to create a stacked bar out of two separate bar charts (Datasets)?

조회 수: 1(최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 8월 4일
댓글: Wolfgang McCormack 2021년 8월 4일
Hi everyone,
I have two 5x6 data available. At the moment, it is being plotted as the following but how can I make them stacked?
Right now I am concating data using > Concat = [Data1;Data2] and then I use > bar(Concat). It does not work with bar(concat, 'stacked') I tried to concat them using horizontal version but that does not work too.
  댓글 수: 4
Wolfgang McCormack
Wolfgang McCormack 2021년 8월 4일
@Simon Chan oh that's exactly what I wanted, could you please tell me how you did that? Thank you
by the way, is ther eany option to keep the color scheme the same as in my chart?

댓글을 달려면 로그인하십시오.

채택된 답변

Simon Chan
Simon Chan 2021년 8월 4일
You may try the following code, but noticed that the XTick is not consistent with your data since there are additional dummy data.
Data1 = round(100*rand(5,6)); % Your data Data1
Data2 = round(100*rand(5,6)); % Your data Data2
dummy = zeros(5,6); % Dummy data
Data1_extend = vertcat(Data1,dummy);
Data2_extend = vertcat(Data2,dummy);
Data_combine = horzcat(Data1_extend(:), Data2_extend(:));
bar(Data_combine,'stacked');
f = gcf;
f.Children.XTick=''; % Remove XTick
Not good at setting colors on bar chart, may be someone provide the solution about that.
  댓글 수: 3
Wolfgang McCormack
Wolfgang McCormack 2021년 8월 4일
@Simon Chan yes I just copy and pasted yours and tried to run it to see what you mean by xtick :D

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by