필터 지우기
필터 지우기

How to plot two bar plot in the same figure?

조회 수: 59 (최근 30일)
Tony Castillo
Tony Castillo 2021년 6월 28일
댓글: Tony Castillo 2021년 6월 29일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by