2nd y axis on bar chart - label and data labels

3 vues (au cours des 30 derniers jours)
Jenny
Jenny le 28 Avr 2014
Modifié(e) : dpb le 28 Avr 2014
I am plotting the total number of observations per direction sector as a bar chart. I have then added another axis to the bar chart to overlay the % of the total number of observations to the bar chart (as a red line with an x marker). My questions are:
1 - how do I label the 2nd y axis ?
2 - how do I add the data labels (% values) to the plot?
Jenny
Data:
Dir Count %
0 3003 3.6
15 3660 4.3
30 3076 3.7
45 3049 3.6
60 3312 3.9
75 4908 5.8
90 7034 8.4
105 6401 7.6
120 4380 5.2
135 3492 4.1
150 1870 2.2
165 1430 1.7
180 870 1.0
195 1145 1.4
210 1859 2.2
225 4641 5.5
240 6087 7.2
255 7184 8.5
270 6258 7.4
285 3793 4.5
300 2410 2.9
315 1478 1.8
330 1256 1.5
345 1635 1.9
hf=figure;
bar(Dircountdata(1:end-1));
ha=gca;
h=findobj(gca,'Type','patch');
grid on
set(ha,'XTickLabel',{'0';'15';'30';'45';'60';'75';'90';'105';'120';'135';'150';'165';'180';'195';'210';'225';'240';'255';'270';'285';'300';'315';'330';'345'},...
'XTick',[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24],'Fontsize',12);
xlabel('Retning (^{\circ})','Fontsize',12);
ylabel('Antall Obs.','Fontsize',12);
hold on
ha2=axes('parent',hf);
plot(ha2,Dirpercdata(1:end-1),'rx:','LineWidth',2)
%set(ha2,'Ylabel','String','Prosent (%)');
set(ha2,'Color','none','YAxisLocation','right','XTick',[],'Box','off');
set(ha2,'YLim',[0 10],'YTick',0:1:10);
set(ha2,'Position',get(ha,'Position'));
hold off

Réponses (1)

dpb
dpb le 28 Avr 2014
How about plotyy instead where most is taken cre for you...
y1=Dircountdata(1:end-1); % just for brevity later
y2=Dirpercdata(1:end-1);
x=1:length(y1); % need x for plotyy
hA=plotyy(x,y1,x,y2,@bar,@plot); % create base plot
Now just fix up the two axes as desired..hA has the handles for them
See
doc plotyy
for more details
  3 commentaires
dpb
dpb le 28 Avr 2014
Modifié(e) : dpb le 28 Avr 2014
Another asked the same ? just a day or so ago...
BTW, to show the data as percent, simply define the format string correctly when creating the text array from the y values--
ytxt=num2str(y1,'%.1f%%');
Need the double-'%' to "escape" the special character meaning in formatting strings. The above will produce an output string like '3.5%' with one decimal point of precision. As always, salt to suit altho that appears to be all the precision your data are.
Sean de Wolski
Sean de Wolski le 28 Avr 2014
Modifié(e) : Sean de Wolski le 28 Avr 2014
Hi Jenny,
This should get you started:
x = 1:3;
y = [17 49 34];
bar(x,y)
ylim([0 100])
labels = strcat(cellstr(num2str(y')),'%'); % convert number to string, string to cell and concatenate % sign
text(x,y+y*0.05,labels,'Rotation',45)

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by