Change distance between grouped bars (bars are overlapping)
30 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Benjamin Kraus
2024-4-26
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 个评论
更多回答(3 个)
Ajay Kumar
2019-11-12
编辑:Ajay Kumar
2019-11-12
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
2019-11-12
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 个评论
Hunter Pruett
2020-5-23
Just finished the barmod function--controls relative position of just about everything in the plot. Hope this helps anyone else with this question.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!