Can I change the width of a single bar in a bar plot?

조회 수: 26 (최근 30일)
Corymbiamaculata
Corymbiamaculata 2022년 3월 14일
댓글: Corymbiamaculata 2022년 3월 14일
I have a grouped bar plot, bb:
bb = bar(x,y)
where x is a 7x1 double vector with month numbers and y is a 7x5 double vector with measurement data. For each of the seven time points, I get five bars with data.
I would like to add some spacing between the first two and the last three bars in each group. One idea I came up with is to add a column of NaNs to y (to make it 7x6 with the third column empty). However, this creates a very wide gap between my bars. I would prefer to make it smaller by changing the width of this 'NaN' bar in the plot while keeping the other bars as they are. Is there a way to do this?

답변 (1개)

Voss
Voss 2022년 3월 14일
Does the gap still appear too wide if you widen the bars?
x = (1:7).';
y = rand(7,5);
new_y = NaN(size(y)+[0 1]);
new_y(:,[1 2 4:end]) = y;
% bb = bar(x,new_y);
bb = bar(x,new_y,'BarWidth',1);
  댓글 수: 3
Adam Danz
Adam Danz 2022년 3월 14일
There's no way to do that using the bar() command with grouped bars.
An ugly workaround may be possible such as plotting all bars individually and computing their x-coordinates but it sounds like a headache and something vulnerable to bugs.
Perhaps you could visually group them using color or xline lines but I haven't thought out the details.
Corymbiamaculata
Corymbiamaculata 2022년 3월 14일
I actually have them grouped by a hatched pattern already, but I'm still not 100% satisfied, that's why I hoped there was a way to add a small gap within the groups.
I can obviously divide the data into two figures, but I hoped to avoid that, since each figure has six subplots. I don't want to multiply entities beyond necessity :)

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by