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

21 次查看(过去 30 天)
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 中查找有关 Line Plots 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by