How to plot two bar plot in the same figure?

Dear all,
I am wondering how to make a figure in matlab composed by two bar plots, the first contentive of several vaklues I want to make this stacked, this is a easy going job, but the problem arise when I attemp to set another bar plot next to the first one (the stacked), I need to make this in order to make a comparison.
I attempted with this but it does not work Y1=bar([U1, 'stacked'], [Mean], barWidth);
In the end, I just want to set a non-stacked bar plot next to the corresponding stacked one, like the black one I have drawn in the figure above. Just to remark it should not be overlapped but I did not have enough spafe in between the stacked ones.
I kindly ask for your insights in this matter.
Best regards

 採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 28 日
編集済み: Scott MacKenzie 2021 年 6 月 29 日

1 投票

There are a few ways to do this. The best option in any situation depends on how your data are organized (or can be organized) and the amount of fiddling necessary to get the desired bar colours, tick labels, etc.
This first solution plots two sets of data, one stacked and the other with single bars. The bar widths and positions are adjusted as per your needs. There is a tick label for each group of unstacked+stacked bars.
n = 5;
x1 = rand(2,n)
x2 = 2 * rand(1,n);
b1 = bar(x1','stacked');
hold on;
b1(1).BarWidth = .4;
b1(1).XData = (1:n) + .25; % move stacked bars right
b1(2).XData = (1:n) + .25;
b2 = bar(x2);
b2.BarWidth = 0.4;
b2.XData = (1:n) - .25; % move single bars left
The second solution expands the two sets of data to include 0s either in the odd positions for the stacked bars or in the even positions for the single bars. Each bar has an x-axis tick and tick label.
n = 10;
odd = ~rem(1:n,2);
even = rem(1:n,2)
x1 = rand(2,n) .* odd;
x2 = 2 * rand(1,n) .* even;
b1 = bar(x1','stacked');
hold on;
b2 = bar(x2);
The third solution uses a single set of data but nulls out the odd entries in one column. All the bars are stacked but half the values in one set are zero.
n = 10;
odd = logical(rem(1:n,2));
x = rand(2,n)';
x(odd,1) = 0; % make every second value 0 in 1st column
bar(x,'stacked');

5 件のコメント

Tony Castillo
Tony Castillo 2021 年 6 月 29 日
Thank you so much for your comprehensible answer, as you can see I got it following your guidelines. I take advantage to just finish the Figure, do yo have any workaround to set the label's colours properly, I mean matching whit their corresponding Bar plot?.
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 29 日
編集済み: Scott MacKenzie 2021 年 6 月 29 日
@Tony Castillo You're welcome. BTW, I adjusted the code in the 1st solution to position the tick labels in the middle of the unstacked+stacked pairs of bars. I can see in your figure that the tick labels are under the stacked bars, so you might want to make this adjustment as well.
As for your question about colours, the issue seems to be with the legend entries themselves. Is "Renewable Generation" the correct label for the green bars or for the stacked bars (which show three colors)? What bar is the "Consumption" label associated with?
Tony Castillo
Tony Castillo 2021 年 6 月 29 日
Thank you for your quick response. In this regard, The unstacked green bar is Consumption, and the stacked with multiple colours is the Renewable generation. I hope I had answered your doubts about the color organization in the Figure to make easier the understanding of my issue.
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 29 日
編集済み: Scott MacKenzie 2021 年 6 月 29 日
Hmm, this is a bit of a challenge. If you add this code to the end of my 1st solution
labels = { 'Consumption' 'Renewable Generation' };
legend([b2 b1(1)], labels);
here's the result:
This is mostly correct. But, of course, there's the new issue that the "Renewable Generation" label shows the color for just one of the stacked bars. So, that's not quite right. I don't know of any way to have >1 color in a signal entry in a legend.
If you're OK to have a label for each bar in the stacked bars, then this will work:
labels = { 'Consumption' 'Blue label' 'Red label' };
legend([b2 b1], labels);
Tony Castillo
Tony Castillo 2021 年 6 月 29 日
Yes it is a bit challenging, but I have addressed it using your first solution, thank you very much for your attitude dealing with this.

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by