![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/702732/image.png)
How to create a stacked bar out of two separate bar charts (Datasets)?
13 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/702657/image.png)
4 个评论
Simon Chan
2021-8-4
You may add some dummy data to separate each group.
However, the XTick may not be consistent with your data and hence I remove it in the following figure.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/702752/image.jpeg)
采纳的回答
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 个评论
Simon Chan
2021-8-4
I have not using any curly brace or dot indexing in the code above.
What are the data type of your 5x6 data?
Did you just try to run my code alone and getting the same error?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!