Change distance between grouped bars (bars are overlapping)
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jesper Nielsen
am 12 Nov. 2019
Beantwortet: Benjamin Kraus
am 26 Apr. 2024
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
1 Kommentar
Akzeptierte Antwort
Benjamin Kraus
am 26 Apr. 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
0 Kommentare
Weitere Antworten (3)
Ajay Kumar
am 12 Nov. 2019
Bearbeitet: Ajay Kumar
am 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
Steven Lord
am 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.
- Increase the size of the axes and decrease the bar width, so the bars are wide enough to "support" the text above them.
- 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.
0 Kommentare
Hunter Pruett
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!