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?

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Stephan
Stephan el 21 de Abr. de 2021
Editada: Stephan el 21 de Abr. de 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)
labels = 10×2 cell array
{'A'} {'C'} {'B'} {'D'} {'C'} {'A'} {'D'} {'B'} {'E'} {'C'} {'F'} {'D'} {'G'} {'A'} {'D'} {'B'} {'A'} {'C'} {'B'} {'D'}
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 comentarios
Stephan
Stephan el 21 de Abr. de 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)'
labels = 10×2 cell array
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'D'} {'A'} {'B'} {'C'} {'D'} {'A'} {'B'} {'C'} {'D'} {'A'} {'B'} {'C'} {'D'}
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
Maja Danovska
Maja Danovska el 21 de Abr. de 2021
Thank you very much Stephan!!
It works finally. A really useful example.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Labels and Annotations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by