Is it possible to set the size of the gap between grouped bars in a bar graph?

94 vues (au cours des 30 derniers jours)
I know you can set the width of each bar (I set mine near max) but I am curious if I can minimize the white space by decreasing the x distance between each pair (group) of bars.
This is what I am plotting:
coh = [0.9862, 0.9773
0.971, 0.9544
0.8969, 0.6791
0.8835, 0.9051
0.8558, 0.6727
0.6727, 0.8641];
figure
hB=bar(coh,.95);
Thank you!

Réponse acceptée

the cyclist
the cyclist le 30 Juin 2020
Modifié(e) : the cyclist le 30 Juin 2020
Another option would be to abandon using grouped bars in a single call to bar(), and instead plot the two sets of bars with two different calls to bar(), and specifying the x locations of those bars.
figure
hold on
w = 0.4;
bar((1:6)-w/2,coh(:,1),w)
bar((1:6)+w/2,coh(:,2),w)
  2 commentaires
Heidi Hirsh
Heidi Hirsh le 30 Juin 2020
I really like this solution for minimizing the gaps between paired bars. Thank you! BUT now I'm not sure how to plot my error bars correctly. I tried splitting up the line that identifies the x location for the error bars but for some reason it doesn't work.
I tried this:
hb1=bar((1:6)-w/2,coh(:,1),w )
hb2=bar((1:6)+w/2,coh(:,2),w )
X1 = cell2mat(get(hb1,'XData')).' + [hb1.XOffset];
X2 = cell2mat(get(hb2,'XData')).' + [hb1.XOffset];
I was hoping I could plot the error bars in two lines like you did the bars. But the lines defining X1 and X2 don't work :(
Heidi Hirsh
Heidi Hirsh le 30 Juin 2020
Actually I realized you (@the cyclist) already solved my problem. This works:
f=figure('units', 'inches', 'position', [0 0 3.5 2.5]) %left, bottom, width, height
% f1=figure(1)
box on
hold on
w = 0.4;
hb1=bar((1:6)-w/2,coh(:,1),w )
hb2=bar((1:6)+w/2,coh(:,2),w )
xlim([.4 6.6])
X1=(1:6)-w/2
X2=(1:6)+w/2
hEB1=errorbar(X1,coh(:,1),errlow(:,1),errhigh(:,1),'.','markersize',0.05,'linewidth',.5,'capsize',0.05);
hEB2=errorbar(X2,coh(:,2),errlow(:,2),errhigh(:,2),'.','markersize',0.05,'linewidth',.5,'capsize',0.05);
set(hEB1,'Color','k')
set(hEB2,'Color','k')
ylabel('Coherence')
set(gca,'fontsize',12)

Connectez-vous pour commenter.

Plus de réponses (2)

Benjamin Kraus
Benjamin Kraus le 26 Avr 2024
Starting in R2024a, you can now customize the width of each group of bars using the new GroupWidth property.
For example:
coh = [0.9862, 0.9773
0.971, 0.9544
0.8969, 0.6791
0.8835, 0.9051
0.8558, 0.6727
0.6727, 0.8641];
figure
hB=bar(coh,.95);
set(hB, GroupWidth = 0.95);

the cyclist
the cyclist le 30 Juin 2020
This question and the answer from MATLAB staff suggest that it is not possible using the built-in bar function.
However, there is another answer from someone who contributed the barmod function to the File Exchange, claiming to solve this problem. It's quite new, and I have not tried it, but it might be worth a shot.
  1 commentaire
Heidi Hirsh
Heidi Hirsh le 30 Juin 2020
Thank you for the ideas! I'm super disappointed that it seems impossible to adjust the offset in the built-in bar function. And I don't think I can use barmod because I ultimately need to include error bars on each bar and it looks like barmod does not allow that. This is my complete code (I simplified it above thinking this might be a simpler fix):
coh = [0.9862, 0.9773
0.971, 0.9544
0.8969, 0.6791
0.8835, 0.9051
0.8558, 0.6727
0.6727, 0.8641];
errhigh = [0.0083, 0.0137
0.0137, 0.0274
0.0611, 0.1797
0.0688, 0.0563
0.1426, 0.1829
0.0875, 0.0799];
errlow = [0.0208, 0.034
0.043, 0.0661
0.1382, 0.3105
0.1533, 0.1288
0.1827, 0.3135
0.1879, 0.1743];
f=figure('units', 'inches', 'position', [0 0 3.5 2.5])
box on
hB=bar(coh,.95);
hold on
X=cell2mat(get(hB,'XData')).' + [hB.XOffset]; %find x locations for error bars
hEB=errorbar(X,coh,errlow,errhigh,'.','markersize',0.05,'linewidth',.5,'capsize',0.05);
set(hEB,'Color','k')
ylabel('Coherence')
set(gca,'fontsize',12)
Disclaimer: This figure will plot TINY. I'm trying to make it to fit a two column 8.5x11 page (in one column).

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by