How to set the space between bars?

조회 수: 71 (최근 30일)
laith Farhan
laith Farhan 2019년 5월 7일
댓글: DGM 2024년 12월 26일
Dear all,
I would like to set up the distance between bars, Is that possible to reduce the distance between them.
md data is: x = [5493,25255;5493,25255]
Help me please....
data1_published.png

답변 (3개)

Walter Roberson
Walter Roberson 2019년 5월 7일
bar position is controlled by the x you pass in to bar().
The third parameter to bar() controls bar width
To get your bars closer together like you illustrate, you will need to either change the x values that you pass in, or else you will need to use a smaller axes so that they crowd together more. Or you could increase your xlim upper limit which would give you a bunch of white space on the right hand side and the bars crowded more together towards the left, I suppose.

Hunter Pruett
Hunter Pruett 2020년 5월 23일
편집: Walter Roberson 2024년 12월 26일
Just finished the following function that I think will help you with this:

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024년 12월 25일
편집: Sulaymon Eshkabilov 2024년 12월 25일
Moving the positions of the data in the bar chart along the x axis to the left or right can be controlled with a few simple additional commands, e.g.
% Random data created
Y = randi([5, 10], 10, 2); % Data # 1 is Column 1 of Y and Data # 2 is Column 2 of Y
figure(1)
bar(Y,'hist')
title('Original')
legend('Data # 1', 'Data # 2')
ylim([0, 11])
figure(2)
H = bar(Y, 'hist');
Move_1 = H(1).Vertices;
Move_1(:,1) = Move_1(:,1) - 2.75; % Move Data # 1's x positions 2.75 points to the left
H(1).Vertices = Move_1;
Move_2 = H(2).Vertices;
Move_2(:,1) = Move_2(:,1) + 1.5; % Move Data # 2's x positions 1.5 points to the right
H(2).Vertices = Move_2;
title('Shifted X value postions')
legend('Data # 1', 'Data # 2')
ylim([0, 11])
  댓글 수: 1
DGM
DGM 2024년 12월 26일
I didn't know bar() could be forced to generate patch() objects.
% the given data, expanded to reduce ambiguity
y = [5493,25255;5493,25255;5493,25255];
hb = bar(y,'hist'); % use 'hist' option to force patch generation
ngroups = size(y,1); % there are other ways to find this
barwidth = min(diff(hb(1).Vertices(4:5:end,1)))/2; % assuming xdata is uniform
% get vertex indices
lidx = (1:3).' + 5*((1:ngroups)-1); % LH edge of each series
ridx = (4:5).' + 5*((1:ngroups)-1); % RH edge of each series
% adjust the LH edge of series 1 based on the RH edge position
for k = 1:ngroups
xref = mean(hb(1).Vertices(ridx(:,k),1));
hb(1).Vertices(lidx(:,k),1) = xref - barwidth;
end
% adjust the RH edge of series 2 based on the LH edge position
for k = 1:ngroups
xref = mean(hb(2).Vertices(lidx(:,k),1));
hb(2).Vertices(ridx(:,k),1) = xref + barwidth;
end
I'm sure that could be simplified, but at least it makes the objective possible. Trying to use the available 'width' parameter doesn't really work beyond unity.
% grouped bars touch at unity width
figure
bar(1:ngroups,y,1);
% but now they overlap, and their offsets are read-only properties
figure
bar(1:ngroups,y,2);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by