How to set the space between bars?

113 views (last 30 days)
laith Farhan
laith Farhan on 7 May 2019
Commented: DGM on 26 Dec 2024
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

Answers (3)

Walter Roberson
Walter Roberson on 7 May 2019
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 on 23 May 2020
Edited: Walter Roberson on 26 Dec 2024
Just finished the following function that I think will help you with this:

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Dec 2024
Edited: Sulaymon Eshkabilov on 25 Dec 2024
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 Comment
DGM
DGM on 26 Dec 2024
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);

Sign in to comment.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!