MATLAB Answers

How to add a second Y axis to a bar chart?

35 views (last 30 days)
desert_scientist90
desert_scientist90 on 14 Aug 2020
Commented: Sara Boznik on 14 Aug 2020
Hi all , I am trying to make a bar chart with readings of particulate matter. Since one of my observations is an outlier my graph does not look right. Is there anyway I could add a second Y axis to my code?
Thanks in advance for your help
%% pdr bar chart
x1=[ 1 2 3 4 5 6 7 8 9 ];
b1=[12.74 33.47; 71.06 29.25; 43.69 17.20;166.75 111.0; 796.17 3266; 519.0 55.27; 7.18 0; 123.25 0; 25.00 0];%% PM 2.5 readings
y1=bar(x1,b1);
xtips1 = y1(1).XEndPoints;
ytips1 = y1(1).YEndPoints;
labels1 = string(y1(1).YData);
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
xtips2= y1(2).XEndPoints;
ytips2 = y1(2).YEndPoints;
labels2 = string(y1(2).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')

Answers (1)

Sara Boznik
Sara Boznik on 14 Aug 2020
Hi!
I recomend you that you use plotyy.
Below you have an example.
[ax,h1,h2] = plotyy(f,[a',b',d',U'],f,I);
set(ax(1),'YLim',[-2 2])
set(ax(2),'YLim',[-15 15])
set(get(ax(1),'Ylabel'),'String','y_1')
set(get(ax(2),'Ylabel'),'String','y_2')
I hope that this will help you somehow. Good luck.
  2 Comments
Sara Boznik
Sara Boznik on 14 Aug 2020
Sorry, I don't know, I am not so experienced here and I am trying to help as much as I can. And ofc it is very useful to learn something new with this MATLAB Answers.
Wish you best of luck.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!