Clear Filters
Clear Filters

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

21 views (last 30 days)
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?

Answers (1)

Voss
Voss on 14 Mar 2022
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 Comments
Adam Danz
Adam Danz on 14 Mar 2022
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 on 14 Mar 2022
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 :)

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!