multi bar labeling plot

118 ビュー (過去 30 日間)
Ali Ali
Ali Ali 2019 年 5 月 16 日
編集済み: Adam Danz 2021 年 12 月 10 日
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 件のコメント
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 日
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
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():
The best solution would be to use a legend
  8 件のコメント
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 日
Hi Ali,
Here is a simple solution to your problem:
A= randi([15, 25], 13, 3); % Insert your data here
H = bar(A); shg
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 日
  1 件のコメント
Adam Danz
Adam Danz 2021 年 8 月 21 日
Cool function, but how does that address the question of how to label the bar groups?


Community Treasure Hunt

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

Start Hunting!

Translated by