How to set the space between bars?
71 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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....

0 commentaires
Réponses (3)
Walter Roberson
le 7 Mai 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.
0 commentaires
Hunter Pruett
le 23 Mai 2020
Modifié(e) : Walter Roberson
le 26 Déc 2024
Just finished the following function that I think will help you with this:
0 commentaires
Sulaymon Eshkabilov
le 25 Déc 2024
Modifié(e) : Sulaymon Eshkabilov
le 25 Déc 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 commentaire
DGM
le 26 Déc 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);
Voir également
Catégories
En savoir plus sur Discrete Data Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




