Change distance between grouped bars (bars are overlapping)

26 vues (au cours des 30 derniers jours)
Jesper Nielsen
Jesper Nielsen le 12 Nov 2019
Hi!
I need to have my bars in my plot set at a specific width, but when I do, they overlap.
I need to create a space between the overlapping bars, or at least remove the overlap.
Hope you can help.
y = [3.6 2.2; 10.8 2.2; 34.9 11.0; 37.3 46.2; 13.3 38.5];
hBar = bar(y,1.2);
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset');
ydt(k1,:) = hBar(k1).YData;
text(ctr(k1,:), ydt(k1,:), sprintfc('%.1f %%', ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',15, 'Color','black','FontName','memoir')
end
untitled.png
  1 commentaire
Bhaskar R
Bhaskar R le 12 Nov 2019
You need to decrease width of the of the bar

Connectez-vous pour commenter.

Réponse acceptée

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:
y = [3.6 2.2; 10.8 2.2; 34.9 11.0; 37.3 46.2; 13.3 38.5];
hBar = bar(y, GroupWidth=1);
for k1 = 1:size(y,2)
ctr = hBar(k1).XEndPoints;
ydt = hBar(k1).YData;
text(ctr, ydt, sprintfc('%.1f %%', ydt), ...
'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end

Plus de réponses (3)

Ajay Kumar
Ajay Kumar le 12 Nov 2019
Modifié(e) : Ajay Kumar le 12 Nov 2019
Remove the bar width field in line 2
y = [3.6 2.2; 10.8 2.2; 34.9 11.0; 37.3 46.2; 13.3 38.5];
hBar = bar(y);
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(1).XData, hBar(k1).XOffset');
ydt(k1,:) = hBar(k1).YData;
text(ctr(k1,:), ydt(k1,:), sprintfc('%.1f %%', ydt(k1,:)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',15, 'Color','black','FontName','memoir')
end
  1 commentaire
Jesper Nielsen
Jesper Nielsen le 12 Nov 2019
That does not solve the problem unfortunately, as I need the bars to be a specific width, for the text on top to be able to fit (which I cannot make smaller because it becomes unreadable in the report I'm making).
Therefore, I need the bar-width to be 1.2 as stated above, but with a gap between them (or at least no overlap).

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 12 Nov 2019
From the documentation for the bar function, specifically the section that deals with the width input argument:
"Bar width, specified as a fraction of the total space available for each bar. The default of 0.8 means the bar width is 80% of the space from the previous bar to the next bar, with 10% of that space on each side.
If the width is 1, then the bars within a group touch one another."
By the definition of width, if it is equal to 1 the bars will touch. If width is greater than 1, the bars will overlap. There's no way around that.
I think you have a couple different options.
  1. Increase the size of the axes and decrease the bar width, so the bars are wide enough to "support" the text above them.
  2. Find an alternate approach for labeling the bars. Specifying the Y ticks at the heights of the bars and turning the Y grid on looks reasonable to me, though because you have two data points (10.8 and 11.0) that are very close I generated a list without that overlap.
% Make the bar plot
y = [3.6 2.2; 10.8 2.2; 34.9 11.0; 37.3 46.2; 13.3 38.5];
hBar = bar(y,0.8);
% Set the Y ticks
U = uniquetol(y(:), 0.01);
yticks(U);
% Turn on the grid
ax = ancestor(hBar(1), 'axes');
ax.YGrid = 'on';
Alternately you could probably adjust option 2 using yyaxis to create the Y grid for the blue bars on the left axes and the Y grid for the orange bars on the right axes. Then you may not need to use uniquetol, at least not for the data you posted.

Hunter Pruett
Hunter Pruett le 23 Mai 2020
Just finished the barmod function--controls relative position of just about everything in the plot. Hope this helps anyone else with this question.

Catégories

En savoir plus sur Graphics Object Programming 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