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
untitled.png

采纳的回答

Benjamin Kraus
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

更多回答(3 个)

Ajay Kumar
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
  1 个评论
Jesper Nielsen
Jesper Nielsen 2019-11-12
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).

请先登录,再进行评论。


Steven Lord
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.
  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 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.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by