Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
single plot/bar axes with different y-scales

Subject: single plot/bar axes with different y-scales

From: B. Schmidt

Date: 18 Aug, 2010 18:39:20

Message: 1 of 7

I am trying to show a daily streamflow hydrograph (plot) and daily precipitation (bar) on the same graph (axes). Furthermore, I would like to show the hydrograph line using a differnt y-axis scale (and label) than the precipitation bars since the value range for the two data can be quite different. Is this possible using MATLAB?

Subject: single plot/bar axes with different y-scales

From: Sean

Date: 18 Aug, 2010 18:48:20

Message: 2 of 7

"B. Schmidt" <bschmidt0123@gmail.com> wrote in message <i4h9co$5ri$1@fred.mathworks.com>...
> I am trying to show a daily streamflow hydrograph (plot) and daily precipitation (bar) on the same graph (axes). Furthermore, I would like to show the hydrograph line using a differnt y-axis scale (and label) than the precipitation bars since the value range for the two data can be quite different. Is this possible using MATLAB?

Absolutely!
a hint:
help plotyy

Subject: single plot/bar axes with different y-scales

From: B. Schmidt

Date: 18 Aug, 2010 18:56:08

Message: 3 of 7

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4h9tk$ld4$1@fred.mathworks.com>...
> "B. Schmidt" <bschmidt0123@gmail.com> wrote in message <i4h9co$5ri$1@fred.mathworks.com>...
> > I am trying to show a daily streamflow hydrograph (plot) and daily precipitation (bar) on the same graph (axes). Furthermore, I would like to show the hydrograph line using a differnt y-axis scale (and label) than the precipitation bars since the value range for the two data can be quite different. Is this possible using MATLAB?
>
> Absolutely!
> a hint:
> help plotyy

very clever... however, plotyy only applies to two line plots. I am interested in plotting lines and bars on the same graph!

Subject: single plot/bar axes with different y-scales

From: Sean

Date: 18 Aug, 2010 19:09:19

Message: 4 of 7

"B. Schmidt" <bschmidt0123@gmail.com> wrote in message <i4hac8$7r2$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4h9tk$ld4$1@fred.mathworks.com>...
> > "B. Schmidt" <bschmidt0123@gmail.com> wrote in message <i4h9co$5ri$1@fred.mathworks.com>...
> > > I am trying to show a daily streamflow hydrograph (plot) and daily precipitation (bar) on the same graph (axes). Furthermore, I would like to show the hydrograph line using a differnt y-axis scale (and label) than the precipitation bars since the value range for the two data can be quite different. Is this possible using MATLAB?
> >
> > Absolutely!
> > a hint:
> > help plotyy
>
> very clever... however, plotyy only applies to two line plots. I am interested in plotting lines and bars on the same graph!

Oh like this:
H = plotyy(0,0,1:10,rand(1,10)+2);
hold on
bar(H(1),rand(5)*10)
axis tight

Subject: single plot/bar axes with different y-scales

From: Steven_Lord

Date: 18 Aug, 2010 20:19:43

Message: 5 of 7



"B. Schmidt" <bschmidt0123@gmail.com> wrote in message
news:i4hac8$7r2$1@fred.mathworks.com...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> <i4h9tk$ld4$1@fred.mathworks.com>...
>> "B. Schmidt" <bschmidt0123@gmail.com> wrote in message
>> <i4h9co$5ri$1@fred.mathworks.com>...
>> > I am trying to show a daily streamflow hydrograph (plot) and daily
>> > precipitation (bar) on the same graph (axes). Furthermore, I would like
>> > to show the hydrograph line using a differnt y-axis scale (and label)
>> > than the precipitation bars since the value range for the two data can
>> > be quite different. Is this possible using MATLAB?
>>
>> Absolutely!
>> a hint:
>> help plotyy
>
> very clever... however, plotyy only applies to two line plots. I am
> interested in plotting lines and bars on the same graph!

Look at the fifth and sixth input arguments described in HELP PLOTYY.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: single plot/bar axes with different y-scales

From: B. Schmidt

Date: 18 Feb, 2011 20:59:05

Message: 6 of 7

Thank you for the subtle hints folks. However, for those who prefer more concrete examples here is a practical example for plotting a line plot and bars on the same axes but with seperate, dual axes and then setting their properties. Enjoy.

axes(handles.yourAxes)

[AX,H1,H2] = plotyy(x1, y1, x2, y2, 'bar', 'plot');

grid on
%set bar graph properties
set(get(AX(1),'Ylabel'),'String','Bar Graph Units', 'FontSize',10)
set(AX(1), 'XTick', 0:2:22);
set(AX(1), 'TickLength', [0 0]);
set(AX(1), 'Xlim', [-1 24]);
set(AX(2), 'XTickLabel', []);
set(AX(1), 'Xgrid', 'on');
set(AX(1), 'Ygrid', 'off');
set(AX(1), 'Ylim', [0 (1 + max(x1) * 1.1)]);
set(AX(1), 'YTickLabel', []);
set(AX(1), 'FontSize',7);
 
%set line plot properties
set(get(AX(2),'Ylabel'),'String','Line Plot Units', 'FontSize', 10)
set(AX(2), 'XTickLabel', []);
set(AX(2), 'Xlim', [-1 24]);
set(AX(2), 'Xgrid', 'off');
set(AX(2), 'Ygrid', 'off');
set(AX(2), 'Ylim', [0 105]);
set(AX(2), 'YTick', 0:25:100);
% set(AX(2), 'YTickLabel', 0:25:100);
set(AX(2), 'YTickLabel', []);
set(AX(2), 'FontSize',7);

%set plotting function properties after it is rendered
set(H1,'EdgeColor', [0 0.5 0.5], 'FaceColor', [0 0 1], 'BarWidth', 1)
set(H2,'Color', [1 0 0],'LineWidth',2)

Subject: single plot/bar axes with different y-scales

From: Y Budi Sulistioadi

Date: 10 Jun, 2013 02:56:07

Message: 7 of 7

"B. Schmidt" wrote in message <ijmmip$o8$1@fred.mathworks.com>...
> Thank you for the subtle hints folks. However, for those who prefer more concrete examples here is a practical example for plotting a line plot and bars on the same axes but with seperate, dual axes and then setting their properties. Enjoy.
>
> axes(handles.yourAxes)
>
> [AX,H1,H2] = plotyy(x1, y1, x2, y2, 'bar', 'plot');
>
> grid on
> %set bar graph properties
> set(get(AX(1),'Ylabel'),'String','Bar Graph Units', 'FontSize',10)
> set(AX(1), 'XTick', 0:2:22);
> set(AX(1), 'TickLength', [0 0]);
> set(AX(1), 'Xlim', [-1 24]);
> set(AX(2), 'XTickLabel', []);
> set(AX(1), 'Xgrid', 'on');
> set(AX(1), 'Ygrid', 'off');
> set(AX(1), 'Ylim', [0 (1 + max(x1) * 1.1)]);
> set(AX(1), 'YTickLabel', []);
> set(AX(1), 'FontSize',7);
>
> %set line plot properties
> set(get(AX(2),'Ylabel'),'String','Line Plot Units', 'FontSize', 10)
> set(AX(2), 'XTickLabel', []);
> set(AX(2), 'Xlim', [-1 24]);
> set(AX(2), 'Xgrid', 'off');
> set(AX(2), 'Ygrid', 'off');
> set(AX(2), 'Ylim', [0 105]);
> set(AX(2), 'YTick', 0:25:100);
> % set(AX(2), 'YTickLabel', 0:25:100);
> set(AX(2), 'YTickLabel', []);
> set(AX(2), 'FontSize',7);
>
> %set plotting function properties after it is rendered
> set(H1,'EdgeColor', [0 0.5 0.5], 'FaceColor', [0 0 1], 'BarWidth', 1)
> set(H2,'Color', [1 0 0],'LineWidth',2)

Thanks for sharing the hints and codes. However, I could not successfully execute above codes. Instead, I modify the previous commands and come up with much simpler codes as follow. Hope this helps someone else.

t is timestamp in datenumber format (daily)
r is rainfall time-series in mm
s is timestamp for discharge (one for each 3 hrs)
d is discharge in m3/s

*replace them with your own dataset*

--- start of code ---
figure(1);
[AX,H1,H2] = plotyy(t,r,s,d,'bar','plot');
% Set bar graph properties
set(get(AX(1),'Ylabel'),'String','Precipitation (mm)','FontSize',8)
set(AX(1),'XTickLabel',[]);
set(AX(1),'FontSize',8);

% Set line graph properties
set(get(AX(2),'Ylabel'),'String','Discharge (m^3/s)','FontSize',8)
set(AX(2),'XTickLabel', []);
set(AX(2),'FontSize',8);

% Print Month and Year as Tick Label
datetick('x','mmm yyyy')

% Print graph title
title('Hyetograph and Hydrograph of Mahakam Gage Station');

--- end of code ---

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us