How to determine the x-axis positions of the bars in a grouped bar chart

조회 수: 67 (최근 30일)
Suppose I make the following bar chart:
figure; bar([1 2],[3 4; 5 6])
How can I programatically find the x-axis locations of each of the bars? I don't want x = [1 2]. I want the actual locations of the blue and yellow bars.

채택된 답변

Allison
Allison 2015년 6월 3일
the cyclist-
I ran into this same problem when switching from Matlab 2011b to 2015b, with the updated graphics engine. After fooling around for a bit, I found that the following code will work (adapted from your original post):
%
mean_velocity = [5 6 7; 8 9 10]; % mean velocity
std_velocity = randn(2,3); % standard deviation of velocity
figure
hold on
hb = bar(1:3,mean_velocity');
% For each set of bars, find the centers of the bars, and write error bars
pause(0.1); %pause allows the figure to be created
for ib = 1:numel(hb)
%XData property is the tick labels/group centers; XOffset is the offset
%of each distinct group
xData = hb(ib).XData+hb(ib).XOffset;
errorbar(xData,mean_velocity(ib,:),std_velocity(ib,:),'k.')
end
I'm not quite sure why the pause is required- it seems that the new graphics engine requires a little bit of time before it will recognize the figure handle properly. I've tried it in other scripts where there is more code in between the plotting and the additional math and it seems to work fine, as well as trying it just via command line, and found the same issue. This should work in any version r2014b+.
  댓글 수: 6
Tasnuba Siddiqui
Tasnuba Siddiqui 2017년 9월 19일
Thank you very much. It's really useful and helped me a lot!
Alex Wooten
Alex Wooten 2021년 3월 16일
+1 for comment about pause. I had plotting code giving me different results when running line-by-line vs all at once, that explains it!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Maxandre Jacqueline
Maxandre Jacqueline 2018년 8월 27일
How to do this for a categorical x axis?

Hossein
Hossein 2017년 11월 16일
편집: Hossein 2017년 11월 16일
Hi, Thank you for your answer, but unfortunately, there is no 'XOffset' property for the handle in Matlab 2017, does anyone have other ideas?
  댓글 수: 2
the cyclist
the cyclist 2017년 11월 16일
편집: the cyclist 2017년 11월 16일
The code in Allison's answer works for me in version R2017b.
Are you sure you don't have some other problem?
Hossein
Hossein 2018년 9월 21일
Mine was 2017a, now I tested on 2017b and it worked. :) Thank you for your reply.

댓글을 달려면 로그인하십시오.


Shivkumar Vishnempet Shridhar
Shivkumar Vishnempet Shridhar 2022년 1월 12일
There is a far simpler solution to this. Just split this into 2 bar plots. I've set the bar width as 0.2, so any x axis position you set, the bar should occupy x-width/2 to x+width/2
figure; bar([0.8 1.8],[3 4],0.2,'FaceColor',[0, 0.4470, 0.7410],'LineWidth',3)
hold on
bar([1.2 2.2],[5 6],0.2,'FaceColor',[0.9290, 0.6940, 0.1250],'LineWidth',3)
  댓글 수: 2
Shivkumar Vishnempet Shridhar
Shivkumar Vishnempet Shridhar 2022년 1월 12일
You can adjust you bar width or position as you please
the cyclist
the cyclist 2022년 1월 12일
This is the answer to a subtly different problem. This is the answer to "How do I place the bars at positions I choose?" I agree that it is easy to know the positions, if you have predefined them.
That is different from "How do I find the positions of the bars, after the MATLAB designers have placed them?"
They both have their uses.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by