Help: How to add a label (cell 1 x 20) to each bar (total 20 bars) in a bar plot where the bar plot has a 1x 2 dimension?
    9 views (last 30 days)
  
       Show older comments
    
Hi everybody, 
I would like to start by saying that I have already searched in the community for an answer to my question, since apparently the topic has been already well explored, but unofrtunately I did not find any satisfactory answer. Secondly, I am not a Matlab expert so So I am asking for your precious help. I would like to add to my bar plot, and specifically to the top of each bar, some labels stored in a cell array (1 x 20). The total number of bars is 20 and each two bars have been plotted with a common x. Here you can find an example of what I want to plot (coming from Excel). 

The generalisation of my code is the folowing one: 
% Definition of x axis 
x = {'CLTref';'CLT1';'CLT5';'M1';'M2';'F1';'F2';'F3';'R1';'R2'};
% Definition of y axis with random values
y1=[1 1 ; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2 ];
% plot
p1=bar(y1);
%set x labels
set(gca, 'XTickLabel',x, 'XTick',1:numel(x))
%%%%% or I could have done also p1=bar(x,y1)! %%%%%%%%%
Now I would like to add these labels to each bar:
labels={'A', 'B', 'C', 'D','E', 'F', 'G', 'D','A', 'B', 'C', 'D','A', 'B','C', 'D','A', 'B', 'C', 'D'}'
;
.....the problem is that with all the codes found here in the community i have a dimension problem. E.g., I copied randomly these code, that many people were suggesting:
-----------------------------------------------------------------------------
hT=[];              % placeholder for text object handles
for i=1:length(p1)  % iterate over number of bar objects
  hT=[hT,text(p1(i).XData+p1(i).XOffset,p1(i).YData,labels(:,i), ...
          'VerticalAlignment','bottom','horizontalalign','center')];
end
----------------------------------------------------------------------------
when I run it I always get an error. I still do not know how to handle the bar object and its properties. If someone has already a solution, I would be grateful. 
Maja
0 Comments
Accepted Answer
  Stephan
      
      
 on 21 Apr 2021
        
      Edited: Stephan
      
      
 on 21 Apr 2021
  
      Try:
labels=reshape({'A', 'B', 'C', 'D','E', 'F', 'G', 'D','A', 'B', 'C', 'D','A', 'B',...
    'C', 'D','A', 'B', 'C', 'D'}',10,2)
or complete:
% Definition of x axis 
x = {'CLTref';'CLT1';'CLT5';'M1';'M2';'F1';'F2';'F3';'R1';'R2'};
% Definition of y axis with random values
y1=[1 1 ; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2 ];
% plot
p1=bar(y1);
%set x labels
set(gca, 'XTickLabel',x, 'XTick',1:numel(x))
%%%%% or I could have done also p1=bar(x,y1)! %%%%%%%%%
labels=reshape({'A', 'B', 'C', 'D','E', 'F', 'G', 'D','A', 'B', 'C', 'D','A', 'B',...
    'C', 'D','A', 'B', 'C', 'D'}',10,2);
hT=[];              % placeholder for text object handles
for i=1:length(p1)  % iterate over number of bar objects
  hT=[hT,text(p1(i).XData+p1(i).XOffset,p1(i).YData,labels(:,i), ...
          'VerticalAlignment','bottom','horizontalalign','center')];
end
2 Comments
  Stephan
      
      
 on 21 Apr 2021
				if you need it in the other order use:
% Definition of x axis 
x = {'CLTref';'CLT1';'CLT5';'M1';'M2';'F1';'F2';'F3';'R1';'R2'};
% Definition of y axis with random values
y1=[1 1 ; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2; 2 2 ];
% plot
p1=bar(y1);
%set x labels
set(gca, 'XTickLabel',x, 'XTick',1:numel(x))
%%%%% or I could have done also p1=bar(x,y1)! %%%%%%%%%
labels=reshape({'A', 'B', 'C', 'D','E', 'F', 'G', 'D','A', 'B', 'C', 'D','A', 'B',...
    'C', 'D','A', 'B', 'C', 'D'},2,10)'
hT=[];              % placeholder for text object handles
for i=1:length(p1)  % iterate over number of bar objects
  hT=[hT,text(p1(i).XData+p1(i).XOffset,p1(i).YData,labels(:,i), ...
          'VerticalAlignment','bottom','horizontalalign','center')];
end
More Answers (0)
See Also
Categories
				Find more on Labels and Annotations in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


