I want to label each three bars as p1, p2 and p3 for all at the base as the pic attached.. i appreciate any help someone can provide.

2 件のコメント

Adam Danz
Adam Danz 2019 年 5 月 16 日
Wouldn't a legend defining the colors make more sense?
Ali Ali
Ali Ali 2019 年 5 月 16 日
you are right.. but i'll manopolate with the colors later.. so it will not be fixed as it now.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2019 年 5 月 16 日
編集済み: Adam Danz 2021 年 12 月 10 日

0 投票

Here's how to locate the center of each grouped bar and label them.
Starting in Matlab R2019B, the center of each bar is stored in
h = bar(___);
h.XEndPoints % x centers
h.YEndPoints % y endpoints
Prior to Matlab R2019B, you can use an undocumented property "XOffset". This was developed and tested in r2019a.
xCnt are the bar centers.
% Generate grouped bar plot
figure()
v = randi(20,12,3);
h = bar(v,.8);
% Get group centers
xCnt = get(h(1),'XData') + cell2mat(get(h,'XOffset')); % XOffset is undocumented!
% Create Tick Labels
xLab = repmat({'p1','p2','p3'},1,numel(xCnt)/3);
% Set individual ticks
set(gca, 'XTick', sort(xCnt(:)), 'XTickLabel', xLab)
Alternatively, you could rotate the x tick labels
set(gca, 'XTick', sort(xCnt(:)), 'XTickLabel', xLab, 'xticklabelrotation', 90)
% Or use xtickangle(): https://www.mathworks.com/help/matlab/ref/xtickangle.html
The best solution would be to use a legend
legend(h,{'p1','p2','p3'})

8 件のコメント

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 16 日
編集済み: Sulaymon Eshkabilov 2019 年 5 月 16 日
My understanding of the problem was just to set colors on each bar that is all. If this is the case, then you need to fix a color in your bar charts as shown above. Otherwise, all done = well, already!
Ali Ali
Ali Ali 2019 年 5 月 17 日
Adam Danz this is just perfect bro.
Adam Danz
Adam Danz 2019 年 5 月 17 日
Glad I could help. I recommend writing a comment in your code noting that xoffset is an undocumented property in case it's ever removed in the future.
Ali Ali
Ali Ali 2019 年 5 月 18 日
thank you for this notiation Adam.. I have another question about the possiblility of making the labels at the base mix of p1,p2,p3 and the iterations numbers..?
Adam Danz
Adam Danz 2019 年 5 月 18 日
編集済み: Adam Danz 2019 年 5 月 18 日
Is the iterations number the group number? Something like this below?
p1 i1
p2 i1
p3 i1
p1 i2
p2 i2
p3 i2
If so, give this a shot.
% Create Tick Labels
xLab = repmat({'p1 i','p2 i','p3 i'},1,numel(xCnt)/3);
xLab = strrep(xLab,'i',strsplit(num2str(repelem(1:size(v,1),1,size(v,2)))));
190517 224750-Figure 1.jpg
Ali Ali
Ali Ali 2019 年 5 月 19 日
like below if you can Adam
Adam Danz
Adam Danz 2019 年 5 月 19 日
編集済み: Adam Danz 2019 年 5 月 19 日
% xLab are the labels for each individual bar. The pattern you want is the following:
% for every odd triplet, you want [nothing, #, nothing]
% and for every even triplet you want [p#, p#, p#].
% We already have the p# labels so we just need to alter the odd number groups.
% Step 1) Identify the odd number groups.
isOdd = repmat(logical([1,1,1,0,0,0]),1,length(xLab)/6);
% Step 2) Create new labels
newLabs = strsplit(sprintf('| |%d| |', 1:2:length(xLab)/3),'|');
newLabs(cellfun(@isempty, newLabs)) = [];
% Step 3) replace the p# labels for odd groups with [nothing, #, nothing]
xLab(isOdd) = newLabs;
% Step 4) assing newlabels to plot
set(gca, 'XTick', sort(xCnt(:)), 'XTickLabel', xLab, 'xticklabelrotation', 90)
Ali Ali
Ali Ali 2019 年 5 月 20 日
thank you a lot Adam.. this is realy helped me.

サインインしてコメントする。

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 16 日

0 投票

Hi Ali,
Here is a simple solution to your problem:
A= randi([15, 25], 13, 3); % Insert your data here
H = bar(A); shg
H(1).FaceColor='g';
H(2).FaceColor='r';
H(3).FaceColor='b';
Good luck.

1 件のコメント

Ali Ali
Ali Ali 2019 年 5 月 17 日
Sulaymon Eshkabilov i think you did not get my point.. anyway thank you for your help.

サインインしてコメントする。

Sergio Yanez-Pagans
Sergio Yanez-Pagans 2021 年 8 月 21 日

0 投票

1 件のコメント

Adam Danz
Adam Danz 2021 年 8 月 21 日
Cool function, but how does that address the question of how to label the bar groups?

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 5 月 16 日

編集済み:

2021 年 12 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by