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

29 vues (au cours des 30 derniers jours)
Corymbiamaculata
Corymbiamaculata le 14 Mar 2022
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?

Réponses (1)

Voss
Voss le 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 commentaires
Adam Danz
Adam Danz le 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 le 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 :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by