How to set the space between bars?
120 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
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.
0 件のコメント
Hunter Pruett
2020 年 5 月 23 日
編集済み: Walter Roberson
2024 年 12 月 26 日
Just finished the following function that I think will help you with this:
0 件のコメント
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
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 Center および File Exchange で Discrete Data Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!